Search code examples
androidwidgetmediaplaybackmediabrowserservicecompat

Android Widget: skip_to_next and skip_to_previous buttons don't work


I am currently trying to develop a widget for an audio app. The widget includes a skipt to previous, a play and a skip to next button like the picture bellow.

enter image description here

I've followed this recommended approach and the play_pause button works correctly, but for the time being only if the playback has started.

To start the playback the user must 1) Open the app. 2) Choose a podcast. 3) Choose an episode to play. 4) Wait for the buffering to complete

Just setting the correct PendingIntent to the play_pause button did the work. So when this button is clicked my MediaSession.Callback.onPlay() method is called.

Unfortunately that's not the case with the skip_to_next and skip_to_previous buttons. I want when the skip_to_next widget button is clicked the MediaSession.Callback.onSkipToNext() to be called.

Here is my AppWidgetProvider class and the MediaBrowserService as well.


Solution

  • I'm not sure if this is the full solution, but it appears that the previous and next buttons were not registered?

    I've not really used the MediaButtonReceiver before so I'm guessing, but I added ACTION_SKIP_TO_PREVIOUS to getPlaybackStateFromPlayerState in the PlayBackUtils now the code does receive the previous event and actually changes to the previous track as well.

    case STARTED:
        return stateBuilder
            .setState(PlaybackStateCompat.STATE_PLAYING, 
                currentPosition, playbackSpeed)
            .setActions(PlaybackStateCompat.ACTION_PAUSE | 
                PlaybackStateCompat.ACTION_PLAY_PAUSE | 
                PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS |
                PlaybackStateCompat.ACTION_STOP | 
                PlaybackStateCompat.ACTION_SEEK_TO | 
                PlaybackStateCompat.ACTION_PLAY)
            .build();