Reputation: 185
Only Previous Button is Worked in NotificationCompat.MediaStyle() in android. Why my next and pause button worked in android notification.
private void setMediaPlaybackState(int state) {
PlaybackStateCompat.Builder playbackstateBuilder = new PlaybackStateCompat.Builder();
if( state == PlaybackStateCompat.STATE_PLAYING ) {
playbackstateBuilder.setActions(PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_PAUSE);
} else {
playbackstateBuilder.setActions(PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_PLAY);
}
playbackstateBuilder.setActions(PlaybackStateCompat.ACTION_SKIP_TO_NEXT);
playbackstateBuilder.setActions(PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS);
playbackstateBuilder.setState(state, PlaybackStateCompat.PLAYBACK_POSITION_UNKNOWN, 1);
mMediaSessionCompat.setPlaybackState(playbackstateBuilder.build());
}
private void showPlayingNotification() {
NotificationCompat.Builder builder = MediaStyleHelper.from(BackgroundAudioService.this, mMediaSessionCompat);
if( builder == null ) {
return;
}
builder.addAction(new NotificationCompat.Action(R.drawable.ic_skip_previous_black_48dp, "Play", MediaButtonReceiver.buildMediaButtonPendingIntent(this, PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS)));
builder.addAction(new NotificationCompat.Action(R.drawable.ic_pause, "Pause", MediaButtonReceiver.buildMediaButtonPendingIntent(this, PlaybackStateCompat.ACTION_PLAY_PAUSE)));
builder.addAction(new NotificationCompat.Action(R.drawable.ic_skip_next_black_48dp, "Play", MediaButtonReceiver.buildMediaButtonPendingIntent(this, PlaybackStateCompat.ACTION_SKIP_TO_NEXT)));
builder.setStyle(new NotificationCompat.MediaStyle().setShowActionsInCompactView(0,1,2).setShowCancelButton(true).setMediaSession(mMediaSessionCompat.getSessionToken()));
builder.setSmallIcon(R.drawable.music_logo);
NotificationManagerCompat.from(BackgroundAudioService.this).notify(1, builder.build());
}
Upvotes: 2
Views: 399
Reputation: 19
I realize that this post is a little old, but I am going to answer with what I think your question is asking. You are saying that only the previous button works, but skip doesn't. My response to this is to do the following for you playbackStateBuilder:
playbackStateBuilder.setActions(PlaybackStateCompat.ACTION_SKIP_TO_NEXT
| PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS);
For whatever reason the way you have it declaring the actions one after another like that doesn't work. I have no idea why. I struggled with this for literally two weeks and no one besides you my friend has posted a question about this. I'm hoping that whoever experiences this will be saved by my response and I hope it helps you too my friend! (:
Upvotes: 2