uamp icon indicating copy to clipboard operation
uamp copied to clipboard

Unstoppable playback after disconnecting from MediaBrowser

Open thibseisel opened this issue 8 years ago • 3 comments

UAMP version: Latest, Kotlin version Android version: Tested on Oreo (API 26+), may also be reproduced on earlier versions.

Issue description

When connecting to UAMP with the Media Controller Test app, I can control the media session and start playback as expected. However, while playback is not stopped when I close the Media Controller Test app, the notification is cleared and I'm unable to control playback anymore. Also, re-establishing a new connection doesn't help regaining control over it.

I think the root cause is that the MusicService is not started, only bound to MediaBrowser clients. When starting playback via UAMP's UI then quitting the application, playback continues because MediaSessionConnection never calls MediaBrowserCompat.disconnect() when the Activity is destroyed, resulting in MusicService not being destroyed (because it still has a bound client). On the other hand, the Media Controller Test app calls MediaBrowserCompat.disconnect() when finishing the MediaAppControllerActivity, which causes the following side-effects:

  1. The last client unbinds from MusicService, triggering MusicService.onDestroy().
  2. As a result from calling MusicService.onDestroy(), the MediaSession is released, preventing further interactions with it.
  3. The notification is cleared, preventing from controlling the MediaSession.
  4. The ExoPlayer instance is neither stopped nor released, thus keeping it playing and leaking valuable resources.

Steps to reproduce

  1. Install UAMP and Media Controller Tester if not installed already.
  2. Open Media Controller Tester and tap "Control" button at the UAMP line.
  3. Click the "Play" button to start playback. Music should now be playing and the notification is displayed.
  4. Tap the device's back button, finishing the current Activity. The notification should clear itself at this moment.

Suggestions

Earlier versions of the UAMP were starting the service when it is put to the foreground. This was coupled with the DelayedStopHandler class that automatically stopped the service after a given delay without being playing. I think this feature should be brought back to this rewritten sample. In addition, I suggest stopping and releasing the player instance before destroying MusicService. A good place to do that might be in MediaControllerCallback.onSessionDestroyed() as the MediaSession is released in onDestroy(), triggering this callback. Also, shouldn't MediaBrowserCompat.disconnect() be called when finishing UAMP's MainActivity ?

I'd like to thank the whole team behind this sample for making it way simpler and readable than its Java older version. Keep going !

thibseisel avatar Sep 16 '18 14:09 thibseisel

The patch I uploaded only addresses the point from MusicService with the smallest patch. I'll leave the issue open, but the biggest problem, having the notification go away while music is playing, should be fixed.

nic0lette avatar Sep 20 '18 20:09 nic0lette

@nic0lette Found the following issue: When I swipe UAMP away from recents, the removeNowPlayingNotification() isn't called because in the method updateNotification() the metadata is already null if(mediaController.metadata == null) return and notification is stuck in the notification tray and control icons also does not work.

picklepickle1 avatar Mar 26 '19 10:03 picklepickle1

The way notifications are handled has changed. This should be fixed now.

dturner avatar Dec 04 '20 12:12 dturner