Commit 868e86cd by bachinger Committed by christosts

Stop service when app is terminated while the player is paused

If the service ever has been started but is not in the foreground, the
service would be terminated without calling onDestroy(). This is because
when onStartCommand returns START_STICKY [1], the app takes the
responsibility to stop the service.

Note that this change interrupts the user journey when paused, because the
notification is removed. Apps can implement playback resumption [2] to give
the user an option to resume playback after the service has been terminated.

[1] https://developer.android.com/reference/android/app/Service#START_STICKY
[2] https://developer.android.com/guide/topics/media/media-controls#supporting_playback_resumption

Issue: androidx/media#175
#minor-release
PiperOrigin-RevId: 492192690
(cherry picked from commit 6a5ac191)
parent 1a9b1c4d
...@@ -72,6 +72,12 @@ class PlaybackService : MediaLibraryService() { ...@@ -72,6 +72,12 @@ class PlaybackService : MediaLibraryService() {
return mediaLibrarySession return mediaLibrarySession
} }
override fun onTaskRemoved(rootIntent: Intent?) {
if (!player.playWhenReady) {
stopSelf()
}
}
override fun onDestroy() { override fun onDestroy() {
player.release() player.release()
mediaLibrarySession.release() mediaLibrarySession.release()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment