Skip to content

Commit

Permalink
Handle background playback more efficiently (#49)
Browse files Browse the repository at this point in the history
* Update onTaskRemoved impl

* Restore current episode playlist's index correctly when played from notifications UI
  • Loading branch information
mr3y-the-programmer authored May 1, 2024
1 parent 6f966e6 commit 075134e
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions app/src/main/kotlin/com/mr3y/podcaster/service/PlaybackService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -117,7 +123,7 @@ class PlaybackService : MediaSessionService() {

MediaItemsWithStartPosition(
mediaItems,
C.INDEX_UNSET,
mediaItems.indexOfFirst { it.mediaId.toLong() == currentlyPlayingEpisode?.episode?.id },
startingPosition,
)
}
Expand Down

0 comments on commit 075134e

Please sign in to comment.