[英]Change play button to pause button in notification
When I press the play button on the notification of the player I developed, I need that play button will be replaced by pause button, and vice versa.当我在我开发的播放器的通知上按下播放按钮时,我需要将播放按钮替换为暂停按钮,反之亦然。 I have, in my Service class the inner Broadcast Receiver class, and the method onPrepared, where I defined the notification.我在我的服务类中有内部广播接收器类和方法 onPrepared,我在其中定义了通知。 In broadcast receiver I tried to do something like this(see the piece of code), but I get Null pointer exception.在广播接收器中,我试图做这样的事情(见代码段),但我得到空指针异常。 How can I change the play button by pause one correctly?如何通过正确暂停播放按钮来更改播放按钮?
enter code here case _Constants.ACTION_PLAY:
Toast.makeText(context, "Play", Toast.LENGTH_SHORT).show();
if(isPlaying())
{
status.contentView.setViewVisibility(R.drawable.icon_pause, View.INVISIBLE);
status.contentView.setViewVisibility(R.drawable.icon_play, View.VISIBLE);
pausePlayer();
}else {
status.contentView.setViewVisibility(R.drawable.icon_play, View.INVISIBLE);
status.contentView.setViewVisibility(R.drawable.icon_pause, View.VISIBLE);
go();
}
Here's the declaration of action buttons inside of method onPrepared:下面是 onPrepared 方法中动作按钮的声明:
enter code here
Intent closeIntent = new Intent(this, NotificationReceiver.class).setAction(_Constants.ACTION_EXIT);
PendingIntent pendingCloseIntent = PendingIntent.getBroadcast(this,0 ,closeIntent,PendingIntent.FLAG_UPDATE_CURRENT);
Intent favoriteIntent = new Intent(this, NotificationReceiver.class).setAction(_Constants.ACTION_FAVORITE);
PendingIntent pendingFavoriteIntent = PendingIntent.getBroadcast(this, 1, favoriteIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent prevIntent = new Intent(this,NotificationReceiver.class).setAction(_Constants.ACTION_PREVIUOS);
PendingIntent pendingPrevIntent = PendingIntent.getBroadcast(this,2,prevIntent,PendingIntent.FLAG_UPDATE_CURRENT);
Intent nextIntent = new Intent(this,NotificationReceiver.class).setAction(_Constants.ACTION_NEXT);
PendingIntent pendingNextIntent = PendingIntent.getBroadcast(this,5,nextIntent,PendingIntent.FLAG_UPDATE_CURRENT);
Intent playIntent = new Intent(this,NotificationReceiver.class).setAction(_Constants.ACTION_PLAY);
PendingIntent pendingPlayIntent = PendingIntent.getBroadcast(this,4,playIntent,PendingIntent.FLAG_UPDATE_CURRENT);
Bitmap artwok = BitmapFactory.decodeResource(getResources(), R.drawable.ic_player);
status =new NotificationCompat.Builder(this,_Constants.CHANNEL_ID)
.setSmallIcon(R.drawable.ic_music)
.setContentTitle(songTitle)
.setContentText(songArtist)
.setLargeIcon(artwok)
.setContentIntent(pendInt)
.setOngoing(true)
//change then null by the required intent to make it work
.addAction(R.drawable.ic_favorite_empty,"Mark Favorite",pendingFavoriteIntent)
.addAction(R.drawable.icon_previous, "Previous",pendingPrevIntent)
.addAction(R.drawable.icon_play,"Play", pendingPlayIntent)
//.addAction(R.drawable.icon_pause,"Pause",pendingPauseIntent)
.addAction(R.drawable.icon_next,"Next",pendingNextIntent)
.setStyle(new androidx.media.app.NotificationCompat.MediaStyle()
.setShowActionsInCompactView(1,2,3)
.setMediaSession(mediaSessionCompat.getSessionToken()))
.addAction(R.drawable.ic_close, "EXIT",pendingCloseIntent)
.setSubText("Now playing...")
.build();
status.flags=Notification.FLAG_ONGOING_EVENT;
startForeground(1,status);
you can create 2 buttons play and pause in the same location in your xml file.您可以在 xml 文件的同一位置创建 2 个按钮播放和暂停。 when you clik on play set its visibility to GONE and make pause button VISIBILITY to VISIBLE and vice versa当您点击播放时,将其可见性设置为 GONE 并将暂停按钮 VISIBILITY 设置为 VISIBLE,反之亦然
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.