[英]Change play button to pause button in notification
当我在我开发的播放器的通知上按下播放按钮时,我需要将播放按钮替换为暂停按钮,反之亦然。 我在我的服务类中有内部广播接收器类和方法 onPrepared,我在其中定义了通知。 在广播接收器中,我试图做这样的事情(见代码段),但我得到空指针异常。 如何通过正确暂停播放按钮来更改播放按钮?
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();
}
下面是 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);
您可以在 xml 文件的同一位置创建 2 个按钮播放和暂停。 当您点击播放时,将其可见性设置为 GONE 并将暂停按钮 VISIBILITY 设置为 VISIBLE,反之亦然
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.