本文主要是介绍畅想音乐播放器第二版,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
很久没更新了,之前说的每周更新一版播放器也没算数,原因是自己实在太忙了,sorry。
播放器最近不会更新了,但是我会开始做一个天气app,这次肯定会记录进展的。
第二版功能分析:
1.继承第一版的所有功能
2.设计一个SQLite数据库,用来记录播放过的音乐
3.加上一个播放队列用来显示已经播放过的音乐
4.加一个Notification,用来显示正在播放的音乐,并且音乐在后台播放的时候,可以通过notification来控制暂停,上一首,下一首。
*数据库设计,用SqlOpenHelper创建一个SQLite数据库
public class SqlOpenHelper extends SQLiteOpenHelper {public SqlOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {super(context, name, factory, version); }public SqlOpenHelper(Context context, String name) {super(context, name, null, 1); }@Override public void onCreate(SQLiteDatabase db) {db.execSQL("create table if not exists musicQueen(_id integer primary key,musicUrl text not null,musicName text ,musicPlayer text,musicSize integer,musicDuration integer)"); }@Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {} }
*自定义一个popupWindow,弹出播放队列
popupWindow内部是一个ListView,ListView的每一item包括歌手名,删除按钮
代码如下:
playQeen.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:id="@+id/playQueen" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/play" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/play_queen" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:textStyle="bold"/> <TextView android:id="@+id/play_count" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/play" android:layout_centerVertical="true" android:textStyle="bold" /> <TextView android:id="@+id/youkuohao" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/play_count" android:layout_centerVertical="true" android:textStyle="bold" android:text=")"/> <ImageButton android:id="@+id/clear_all" android:layout_width="40dp" android:layout_height="40dp" android:padding="0dp" android:src="@drawable/play_queen_delete" android:background="@color/transparent" android:layout_alignParentRight="true" android:focusable="false" android:focusableInTouchMode="false" /> </RelativeLayout> <ListView android:id="@+id/play_queue" android:layout_width="match_parent" android:layout_height="200dp" android:layout_below="@+id/playQueen" ></ListView> </RelativeLayout>
弹出一个popupWindow
/** * 弹出一个PopupWindow,显示播放队列 */ public void popupWindow(){popupView=LayoutInflater.from(getActivity()).inflate(R.layout.play_queen,null); popupWindow=new PopupWindow(popupView, ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.WRAP_CONTENT,true); playCount= (TextView) popupView.findViewById(R.id.play_count); setAdapter(); playQeenDeleteLisener(); setItemClickListener(); playCount.setText(queenBeens.size()+""); // popupWindow.setFocusable(true); popupWindow.setBackgroundDrawable(new BitmapDrawable(BitmapFactory.decodeResource(getResources(),R.mipmap.play_queen_bg))); popupWindow.showAtLocation(popupView, Gravity.BOTTOM,0,0); popupView.setOnKeyListener(new View.OnKeyListener() {@Override public boolean onKey(View v, int keyCode, KeyEvent event) {if (event.KEYCODE_BACK==keyCode){popupWindow.dismiss(); }else if (keyCode==event.KEYCODE_VOLUME_UP){popupWindow.showAsDropDown(view); return true; }return false; }}); }/** * 为popupView设置适配器 */ public void setAdapter(){playQueen= (ListView)popupView.findViewById(R.id.play_queue); queenBeens=getPopupViewData(); adapter=new MyPlayQueenAdapter(getActivity(),queenBeens); playQueen.setAdapter(adapter); }/** * 为popupView获取数据 * _id integer primary key,musicUrl text not null,musicName text ,musicPlayer text,musicSize integer,musicDuration integer */ public ArrayList<MusicQueenBean> getPopupViewData(){ArrayList<MusicQueenBean> list=new ArrayList<>(); sqlOpenHelper=new SqlOpenHelper(getActivity(),"changxiang_music.db"); database=sqlOpenHelper.getReadableDatabase(); Cursor cursor=database.query("musicQueen",null,null,null,null,null,null); if (cursor!=null){for (int i=0;i<cursor.getCount();i++){cursor.moveToNext(); MusicQueenBean bean=new MusicQueenBean(); bean.set_id(cursor.getLong(cursor.getColumnIndex("_id"))); bean.setMusicUrl(cursor.getString(cursor.getColumnIndex("musicUrl"))); bean.setMusicName(cursor.getString(cursor.getColumnIndex("musicName"))); bean.setMusicPlayer(cursor.getString(cursor.getColumnIndex("musicPlayer"))); bean.setMusicSize(cursor.getLong(cursor.getColumnIndex("musicSize"))); bean.setMusicDuration(cursor.getLong(cursor.getColumnIndex("musicDuration"))); list.add(bean); }}return list; }/** * 监听删除播放队列按钮 */ public void playQeenDeleteLisener(){playQeenDelete= (ImageButton) popupView.findViewById(R.id.clear_all); playQeenDelete.setOnClickListener(new View.OnClickListener() {@Override public void onClick(View v) {queenBeens.clear(); clearAllInDataBase(); adapter.notifyDataSetChanged(); }}); }/** *清除播放队列数据库中的一切 */ public void clearAllInDataBase(){database.delete("musicQueen",null,null); }/** * 设置播放队列的点击事件 */ public void setItemClickListener(){playQueen.setOnItemClickListener(new AdapterView.OnItemClickListener() {@Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) {Intent intent=new Intent(); intent.setAction(Constant.playQueenClick); intent.putExtra(Constant.musicUri,queenBeens.get(position).getMusicUrl()); getActivity().sendBroadcast(intent); musicAlbum.setImageBitmap(MediaUtils.getAlbumBitmap(getActivity(),queenBeens.get(position).get_id())); musicName.setText(queenBeens.get(position).getMusicName()); playerName.setText(queenBeens.get(position).getMusicPlayer()); playButton.setImageResource(R.mipmap.stop); StaticData.isPlaying=true; StaticData.isPause=false; }}); }
*自定义一个Notification
代码如下:
Notification_item.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/notification" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="0dp" android:background="@color/black_half_transparent" android:clickable="true"> <ImageView android:id="@+id/not_musicAlbum" android:layout_width="70dp" android:layout_height="70dp" android:background="@mipmap/music_album" /> <TextView android:id="@+id/not_musicName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="音乐名" android:layout_toRightOf="@+id/not_musicAlbum" android:layout_marginLeft="10dp" android:textColor="@color/half_transparent" android:singleLine="true" android:textSize="16sp" /> <TextView android:id="@+id/not_musicAritist" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/not_musicName" android:text="歌手" android:layout_toRightOf="@+id/not_musicAlbum" android:layout_marginLeft="10dp" android:textSize="12sp" android:textColor="@color/half_transparent" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/not_musicAritist" android:layout_toRightOf="@+id/not_musicAlbum"> <ImageButton android:id="@+id/not_prebtn" android:layout_width="25dp" android:layout_height="25dp" android:layout_marginLeft="8dp" android:background="@color/transparent" android:src="@drawable/pre_btn_click" /> <ImageButton android:id="@+id/not_play" android:layout_width="25dp" android:layout_height="25dp" android:layout_toRightOf="@+id/not_prebtn" android:src="@drawable/not_pause_click" android:background="@color/transparent" android:layout_marginLeft="25dp" /> <ImageButton android:id="@+id/not_next" android:layout_width="25dp" android:layout_height="25dp" android:layout_toRightOf="@+id/not_play" android:src="@drawable/not_next_click" android:background="@color/transparent" android:layout_marginLeft="25dp" /> </RelativeLayout> </RelativeLayout>
创建Notification并实现它的数据
/** * 创建通知栏 */ public void createNotification(){builder=new NotificationCompat.Builder(getApplicationContext()); builder.setSmallIcon(R.mipmap.icon_player); builder.setTicker(getString(R.string.notification_ticker)); notification=builder.build(); remoteViews=new RemoteViews(getPackageName(),R.layout.notification_item); /** * 重置里面的UI */ resetNotification(remoteViews); /** * 设置监听 */ setNotificationListener(); notification.contentView=remoteViews; notification.flags=Notification.FLAG_NO_CLEAR; //设置通知或者滑动式不被清除 notificationManager= (NotificationManager) getApplicationContext().getSystemService(Service.NOTIFICATION_SERVICE); notificationManager.notify(notificationId,notification); } /** * 重置通知栏里面的UI */ public void resetNotification(RemoteViews remoteViews){Bitmap bitmap=MediaUtils.getAlbumBitmap(getApplicationContext(),list.get(StaticData.position).getId()); remoteViews.setImageViewBitmap(R.id.not_musicAlbum,bitmap); remoteViews.setTextViewText(R.id.not_musicName,list.get(StaticData.position).getTitle()); remoteViews.setTextViewText(R.id.not_musicAritist,list.get(StaticData.position).getArtist()); if (StaticData.isPlaying){remoteViews.setImageViewResource(R.id.not_play,R.mipmap.widget_pause_button_default); }else if (StaticData.isPause){remoteViews.setImageViewResource(R.id.not_play,R.mipmap.widget_play_button_default); } } /** * 设置通知栏上按钮的监听事件 */ public void setNotificationListener(){//上一首按钮 Intent preIntent=new Intent(); preIntent.setAction(Constant.NOTI_PREMUSIC); PendingIntent prePreIntent=PendingIntent.getBroadcast(getApplicationContext(),0,preIntent,0); remoteViews.setOnClickPendingIntent(R.id.not_prebtn,prePreIntent); //播放按钮 Intent playIntent=new Intent(); playIntent.setAction(Constant.NOTI_PLAYMUSIC); PendingIntent playPreIntent=PendingIntent.getBroadcast(getApplicationContext(),0,playIntent,0); remoteViews.setOnClickPendingIntent(R.id.not_play,playPreIntent); //下一首按钮 Intent nextIntent=new Intent(); nextIntent.setAction(Constant.NOTI_NEXTMUSIC); PendingIntent nextPreIntent=PendingIntent.getBroadcast(getApplicationContext(),0,nextIntent,0); remoteViews.setOnClickPendingIntent(R.id.not_next,nextPreIntent); Intent intent=new Intent(this,MainActivity.class); PendingIntent pendingIntent=PendingIntent.getActivity(getApplicationContext(),0,intent,0); remoteViews.setOnClickPendingIntent(R.id.notification,pendingIntent); } /** * 状态栏上播放方法 */ public void notificationPlay(){if (StaticData.isPlaying){StaticData.isPlaying=false; StaticData.isPause=true; remoteViews.setImageViewResource(R.id.not_play,R.mipmap.widget_play_button_default); mediaPlayer.pause(); }else if (StaticData.isPause){StaticData.isPlaying=true; StaticData.isPause=false; remoteViews.setImageViewResource(R.id.not_play,R.mipmap.widget_pause_button_default); mediaPlayer.start(); } }
还是那句话,需要源代码的联系QQ 2372126585 ,最好是发邮件。什么东西都需要交流学习,闭关锁国是不行的。
这篇关于畅想音乐播放器第二版的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!