2

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.

Themelis
  • 4,048
  • 2
  • 21
  • 45
  • Is it working? Looks like there is a wee bug: "PendingIntent nextPendingIntent = MediaButtonReceiver.buildMediaButtonPendingIntent(context, PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS); setTransportControlOnClickListeners". It should be ACTION_SKIP_TO_NEXT? – Elletlar Jul 17 '18 at 15:04
  • I fixed that bug but that is not the problem. The problem is that the MediaSession.Callback.onSkipeToNext() or MediaSession.Callback.onSkipeToPrevious() is not triggered. – Themelis Jul 17 '18 at 16:05
  • Okay I'll take a quick look... – Elletlar Jul 17 '18 at 16:06
  • Thanks for checking out @Elletlar, it's really weird. Btw I've just pushed the fix on github. – Themelis Jul 17 '18 at 16:08
  • 1
    I ended up doing a Ctrl-Shift-F to see how ACTION_PLAY_PAUSE is used since that one worked and then did the same thing again for ACTION_SKIP_TO_PREVIOUS. The Android Studio IDE has a really clean interface for browsing through strings in multiple files... – Elletlar Jul 17 '18 at 16:56
  • Wow I had no idea about that useful shortcut. So far I used bash commands for this, like cat $(find app -name *java) | grep 'ACTION_PLAY_PAUSE' – Themelis Jul 17 '18 at 17:00
  • It is a really useful command. I'm not sure if you've planned for this yet. But when the app is playing music in the background it is going to need to be a foreground service and will be required to post an ongoing notification in the status bar. It will prevent Android from putting the background service to sleep while it is playing. But with a little work that ongoing notification can be made to look a lot like the home screen widget. It will probably use the same pending intents and infrastructure as the widget as well... – Elletlar Jul 17 '18 at 17:17
  • I have implement that notification following the official tutorials but without complete success. I believe I had post also a question about it but I didn't got any answers. I will check it again later of course. Obviously you are most welcomed to take a look -> https://stackoverflow.com/questions/51023967/mediastylenotification-play-pause-button-does-not-respond-to-click – Themelis Jul 18 '18 at 00:53
  • 1
    Ah okay I saw the foreground bits in the code, but there wasn't any foreground notification while the music was playing. I'll have a look when I get a chance. Cheers. – Elletlar Jul 18 '18 at 22:42
  • Thanks a lot, cheers! – Themelis Jul 19 '18 at 12:21

1 Answers1

2

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();
Elletlar
  • 3,136
  • 7
  • 32
  • 38