基于android的网络音乐播放器-通知栏控制(RemoteViews)(十)
来源:互联网 发布:魔音通话软件 编辑:程序博客网 时间:2024/05/21 09:57
到这里音乐播放器该有的功能基本都有了,最后再添加一个通知栏控制功能——当我们后台运行的时候可以在通知栏看到我们的音乐播放状态并且可以控制音乐的暂停/播放/上一首/下一首;点击通知图标即可返回音乐播放器。要实现通知栏的显示功能需要用到Notification,他的视图是通过使用RemoteViews来实现的,Notification的显示和取消需要通过NotificationManager来控制。下面代码实现了通知栏的显示:
public class MainActivity extends Activity { ...... private NotificationManager nm; private RemoteViews contentViews; private Notification notify; private int NOTIFICATION_ID = 123; private boolean showNotification; ...... protected void onCreate(Bundle savedInstanceState) { ...... initNotification(); }private void initNotification() { //NotificationManager的获取 nm = (NotificationManager)this.getSystemService(NOTIFICATION_SERVICE); Intent mainIntent = new Intent(MainActivity.this, MainActivity.class); PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, mainIntent, 0); notify = new Notification(); notify.when = System.currentTimeMillis(); notify.icon = R.drawable.music; notify.contentIntent = pi;//点击通知跳转到MainActivity notify.flags = Notification.FLAG_AUTO_CANCEL; contentViews = new RemoteViews(getPackageName(), R.layout.notification); contentViews.setOnClickPendingIntent(R.id.playtag, pi); contentViews.setOnClickPendingIntent(R.id.currentmusic, pi); //上一首图标添加点击监听 Intent previousButtonIntent = new Intent(ACTION_PRE_SONG); PendingIntent pendPreviousButtonIntent = PendingIntent.getBroadcast(this, 0, previousButtonIntent, 0); contentViews.setOnClickPendingIntent(R.id.pre, pendPreviousButtonIntent); //播放/暂停添加点击监听 Intent playPauseButtonIntent = new Intent(ACTION_PLAY_AND_PAUSE); PendingIntent playPausePendingIntent = PendingIntent.getBroadcast(this, 0, playPauseButtonIntent, 0); contentViews.setOnClickPendingIntent(R.id.playandpause, playPausePendingIntent); //下一首图标添加监听 Intent nextButtonIntent = new Intent(ACTION_NEXT_SONG); PendingIntent pendNextButtonIntent = PendingIntent.getBroadcast(this, 0, nextButtonIntent, 0); contentViews.setOnClickPendingIntent(R.id.next, pendNextButtonIntent); //退出监听 Intent exitButton = new Intent(ACTION_EXIT); PendingIntent pendingExitButtonIntent = PendingIntent.getBroadcast(this,0,exitButton,0); contentViews.setOnClickPendingIntent(R.id.close,pendingExitButtonIntent); }private void showNotification() { showNotification = true; if(isPlaying){ contentViews.setImageViewResource(R.id.playandpause,android.R.drawable.ic_media_pause); } else{ contentViews.setImageViewResource(R.id.playandpause,android.R.drawable.ic_media_play); } contentViews.setTextViewText(R.id.currentmusic, currentMusicTitle + "—" + currentMusicArtist); String filePath = MainActivity.downloadedPath + "/album/"+ currentMusicTitle + "-" + currentMusicArtist + ".jpg"; if (new File(filePath).exists()) { Bitmap bitmap = BitmapUtil.getScropBitmap(filePath, 60, 60); contentViews.setImageViewBitmap(R.id.playtag, bitmap); } notify.contentView = contentViews; nm.notify(NOTIFICATION_ID, notify);//调用notify方法后即可显示通知 }private BroadcastReceiver playMusicReceiver = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { String action = intent.getAction(); Log.d(TAG, "action = " + action); if (action.equals(ACTION_NEXT_SONG)) { nextSong(); } else if (action.equals(ACTION_PLAY_SONG)) { playMusic(currentMusicPos); } else if (action.equals(ACTION_PAUSE)) { pauseMusic(); } else if (action.equals(ACTION_PRE_SONG)) { preSong(); } else if (action.equals(ACTION_CONTINUE_PLAYING_SONG)) { continuePlaying(); } else if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {//在播放器中点击home键时显示通知栏图标 showNotification(); } else if (action.equals(ACTION_EXIT)) {//通知栏点击退出图标 finish(); } else if (action.equals(ACTION_PLAY_AND_PAUSE)) { if (pause) { continuePlaying(); pause = false; } else if (isPlaying) { pauseMusic(); } else { playMusic(currentMusicPos); } } } };@Override protected void onStart() { //回到音乐播放器时关闭通知 if (showNotification) { nm.cancel(NOTIFICATION_ID); showNotification = false; } updatePlayMusicInfo(); super.onStart(); }Notification的显示和关闭主要是通过NotificationManager.notify()和NotificationManager.cancel()实现的。}
Notification的布局文件notification.xml如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/notification" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:gravity="center_vertical" android:background="#65cb65" android:alpha="0.7"> <!--android:background="#65cb65" --> <ImageView android:id="@+id/playtag" android:layout_width="0dp" android:layout_height="60dp" android:layout_weight="1" android:src="@drawable/launcher" android:layout_marginLeft="5dp" /> <TextView android:id="@+id/currentmusic" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="3" android:layout_marginLeft="10dp" android:gravity="center_vertical" android:textSize="16dp" android:textColor="#f00" android:singleLine="true" android:ellipsize="marquee" android:marqueeRepeatLimit="marquee_forever" android:scrollHorizontally = "true" android:focusable ="true" android:focusableInTouchMode="true"/> <ImageButton android:id="@+id/pre" android:layout_width="0dp" android:layout_height="wrap_content" android:src="@android:drawable/ic_media_previous" android:layout_weight="1" android:background="#dd00dd"/> <ImageButton android:id="@+id/playandpause" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_weight="1" android:background="#dd00dd" android:src="@android:drawable/ic_media_play" /> <ImageButton android:id="@+id/next" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginRight="10dp" android:layout_weight="1" android:background="#dd00dd" android:src="@android:drawable/ic_media_next" /> <ImageView android:id="@+id/close" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:src="@android:drawable/ic_delete" /></LinearLayout>
音乐播放器已完成,下载地址:
Android音乐播放器
0 0
- 基于android的网络音乐播放器-通知栏控制(RemoteViews)(十)
- 基于android的网络音乐播放器-播放控制界面(九)
- Android 音乐播放器的开发教程(十)通知栏Notification的使用 ----- 小达
- Android实战 - 音心播放器 (通知实现音乐的播放/暂停/下一曲控制)
- 基于android的网络音乐播放器-网络音乐的搜索和展示(五)
- 基于android的网络音乐播放器-网络音乐的多线程下载(六)
- 基于android的网络音乐播放器-回调实现音乐播放及音乐收藏的实现(三)
- 基于android的网络音乐播放器-播放音乐及收藏音乐的效果展示(四)
- Android开发:用通知栏(Notification Bar)控制音乐的后台播放
- Android 音乐播放器的通知栏
- 基于android的网络音乐播放器-下载完成后下拉音乐列表刷新(八)
- android-----音乐播放器的音量控制功能(开发)
- Android本地及网络音乐播放器-网络音乐的搜索(三)
- Android本地及网络音乐播放器-网络音乐的试听和下载(四)
- 基于android的网络音乐播放器-下载任务的暂停/继续/删除以及断点下载(七)
- 基于android的网络音乐播放器-添加viewpager和fragment实现滑动切换多个界面(二)
- Android开发本地及网络Mp3音乐播放器(二十)歌曲下载完成后通知主界面更新本地音乐
- 基于Socket编程的远程控制PC音乐播放器App(一)
- MySQL端口
- C语言第二课 数字的运算
- CEF 实现全屏播放视频
- JavaScript性能优化小窍门汇总(含实例)
- Socket通信
- 基于android的网络音乐播放器-通知栏控制(RemoteViews)(十)
- <Effective Mordern C++>笔记:Item 9:prefer alias declarations to typedefs.
- 值传递、地址传递与引用传递的使用
- 实训4/6 Python 字典、元组、set集合
- 常见的异常类型
- 两个指针相减的结果
- 常见的异常类型
- SSH整合applicationContext.xml配置文件(my project)
- Tinker使用过程及注意点(2)-360加固及多渠道分包