play/pause button image in notification ,Android

前端 未结 4 511
时光取名叫无心
时光取名叫无心 2020-12-08 22:32

I have implemented music player which fires a custom notification when stream audio is playing.

Everything is working and I can play/pause the audio using the button

4条回答
  •  生来不讨喜
    2020-12-08 23:10

    This should work. Just make your notification and notificationManager a static global variable.

    public class RemoteControlReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {  
    
            if(action.equalsIgnoreCase("com.example.test.ACTION_PLAY")){
                if(mediaplayer.isPlaying()){
                    mediaplayer.pause();
                    notificationView.setImageViewResource(R.id.button1, R.drawable.play);
                    notification.contentView = notificationView;
                    notificationManager.notify(1, notification);
                }
                else {
                    mediaplayer.start();
                    notificationView.setImageViewResource(R.id.button1, R.drawable.pause);
                    notification.contentView = notificationView;
                    notificationManager.notify(1, notification);
                }
            }
        }
    }
    

提交回复
热议问题