fix(android): keep service alive when UPnP is playing -- override playWhenReady
android / Build + lint + test (push) Successful in 3m53s

This commit is contained in:
2026-06-03 23:51:54 -04:00
parent 47b0894ad6
commit 85926f4ec0
2 changed files with 13 additions and 1 deletions
@@ -218,6 +218,14 @@ class MinstrelForwardingPlayer(
override fun getPlaybackState(): Int =
if (isRemote()) Player.STATE_READY else super.getPlaybackState()
// Mirror remote state so any consumer that gates on playWhenReady --
// notably MediaSessionService's foreground-keepalive checks and our own
// onTaskRemoved -- sees the remote renderer as the source of truth.
// Without this, swiping the app away with Sonos playing would stop the
// service, kill the poll loop, and leave Sonos orphaned.
override fun getPlayWhenReady(): Boolean =
if (isRemote()) remoteState.isPlaying else super.getPlayWhenReady()
override fun release() {
pollJob?.cancel()
scope.cancel()
@@ -176,7 +176,11 @@ class MinstrelPlayerService : MediaSessionService() {
override fun onTaskRemoved(rootIntent: Intent?) {
val player = mediaSession?.player ?: return super.onTaskRemoved(rootIntent)
val activelyPlaying = player.playWhenReady && player.playbackState != Player.STATE_ENDED
// player.isPlaying is overridden on MinstrelForwardingPlayer to return
// remoteState.isPlaying while UPnP is active, so a swipe-away with
// Sonos playing keeps the service (and its UPnP poll loop) alive.
val activelyPlaying = player.isPlaying ||
(player.playWhenReady && player.playbackState != Player.STATE_ENDED)
if (!activelyPlaying) {
stopSelf()
}