diff --git a/app/src/main/kotlin/com/mr3y/podcaster/service/PlaybackService.kt b/app/src/main/kotlin/com/mr3y/podcaster/service/PlaybackService.kt index 321ad492..bc6b6586 100644 --- a/app/src/main/kotlin/com/mr3y/podcaster/service/PlaybackService.kt +++ b/app/src/main/kotlin/com/mr3y/podcaster/service/PlaybackService.kt @@ -67,24 +67,30 @@ class PlaybackService : MediaSessionService() { } override fun onTaskRemoved(rootIntent: Intent?) { + super.onTaskRemoved(rootIntent) val player = mediaSession?.player ?: return - if (!player.playWhenReady || player.mediaItemCount == 0) { + if (!player.playWhenReady) { + release() stopSelf() } } override fun onDestroy() { + release() + super.onDestroy() + } + + override fun onGetSession(controllerInfo: MediaSession.ControllerInfo): MediaSession? = mediaSession + + private fun release() { mediaSession?.run { player.release() release() mediaSession = null } serviceScope.cancel() - super.onDestroy() } - override fun onGetSession(controllerInfo: MediaSession.ControllerInfo): MediaSession? = mediaSession - private inner class PlaybackMediaSessionCallback : MediaSession.Callback { override fun onConnect( session: MediaSession, @@ -117,7 +123,7 @@ class PlaybackService : MediaSessionService() { MediaItemsWithStartPosition( mediaItems, - C.INDEX_UNSET, + mediaItems.indexOfFirst { it.mediaId.toLong() == currentlyPlayingEpisode?.episode?.id }, startingPosition, ) }