基于android的网络音乐播放器-通知栏控制(RemoteViews)(十)

到这里音乐播放器该有的功能基本都有了,最后再添加一个通知栏控制功能——当我们后台运行的时候可以在通知栏看到我们的音乐播放状态并且可以控制音乐的暂停/播放/上一首/下一首;点击通知图标即可返回音乐播放器。
要实现通知栏的显示功能需要用到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音乐播放器

  • 2
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
显示如何实现? 在 Android 中,可以使用 NotificationCompat.Builder 类创建一个带有自定义布局的通知。以下是实现在通知中显示音乐播放器控制的步骤: 1. 创建一个自定义布局,其中包含音乐播放器控制按钮(如播放、暂停、下一首、上一首等)和歌曲信息(如歌曲名、歌手名等)。 2. 使用 NotificationCompat.Builder 创建一个通知,并将自定义布局设置为通知RemoteViews。 3. 在自定义布局中为每个控制按钮设置 PendingIntent。当用户点击按钮时,将触发 PendingIntent 中指定的操作。例如,当用户点击播放按钮时,将触发 PendingIntent 播放音乐。 4. 在通知中显示歌曲信息,例如歌曲名、歌手名等。 5. 最后,将通知发送到通知。 以下是示例代码: ``` // 创建自定义布局 RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification_layout); // 设置每个控制按钮的 PendingIntent Intent playIntent = new Intent(this, MusicService.class); playIntent.setAction(ACTION_PLAY); remoteViews.setOnClickPendingIntent(R.id.play_button, PendingIntent.getService(this, 0, playIntent, 0)); Intent pauseIntent = new Intent(this, MusicService.class); pauseIntent.setAction(ACTION_PAUSE); remoteViews.setOnClickPendingIntent(R.id.pause_button, PendingIntent.getService(this, 0, pauseIntent, 0)); Intent nextIntent = new Intent(this, MusicService.class); nextIntent.setAction(ACTION_NEXT); remoteViews.setOnClickPendingIntent(R.id.next_button, PendingIntent.getService(this, 0, nextIntent, 0)); // 在通知中显示歌曲信息 NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(R.drawable.ic_music_note) .setContent(remoteViews) .setContentTitle("歌曲名") .setContentText("歌手名") .setPriority(NotificationCompat.PRIORITY_LOW); // 发送通知 NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); notificationManager.notify(notificationId, builder.build()); ``` 注意,上面的代码中 `MusicService` 是一个后台服务,用于播放音乐。在 `MusicService` 中实现 `ACTION_PLAY`、`ACTION_PAUSE`、`ACTION_NEXT` 等操作。另外,`CHANNEL_ID` 是通知渠道的 ID,需要在应用中先创建通知渠道。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值