Modern Apps git

← Modern-Apps.git

music: seed playback state when the controller connects

Commit
fe946ba28f38ffcc344c28807c8bf5782fb25aaf
Author
Vayun Mathur <[email protected]>
When
2026-11-07 20:14 UTC (28 days ago)
Parents
fd2ccf2
Symptom: reopen the app while music is already playing (after the
UI process was killed, or any reconnect) and the mini-player showed
no track / paused / repeat off until the track changed or the mode
was toggled, because the observables only moved on player events.

Root cause: PlaybackManager registered a Player.Listener on connect
but never read the controller's current state, so every StateFlow
kept its constructed default for an already-playing session.

Fix: seed isPlaying, current item, shuffle, repeat, position and
duration from the controller immediately on connect, before the
listener is added.

Verification: compile plus module unit tests after this series.
PlaybackManagerTest's no-controller cases are unaffected.

Diff

music/src/main/java/com/vayunmathur/music/platform/PlaybackManager.kt
diff --git a/music/src/main/java/com/vayunmathur/music/platform/PlaybackManager.kt b/music/src/main/java/com/vayunmathur/music/platform/PlaybackManager.ktindex 2381459552..a78673f62d 100644--- a/music/src/main/java/com/vayunmathur/music/platform/PlaybackManager.kt+++ b/music/src/main/java/com/vayunmathur/music/platform/PlaybackManager.kt@@ -113,6 +113,19 @@ class PlaybackManager private constructor(context: Context) {         controllerFuture.addListener({             try {                 controller = controllerFuture.get().apply {+                    // Seed from the player's current state: the listener+                    // below only fires on changes, so connecting while the+                    // service was already playing (app restart, process+                    // death) otherwise left every observable at its default+                    // - paused, no track, repeat off - until the next+                    // transition, and the mini-player offered to play a+                    // track that was already playing.+                    _isPlaying.value = isPlaying+                    _currentMediaItem.value = currentMediaItem+                    _shuffleMode.value = shuffleModeEnabled+                    _repeatMode.value = repeatMode+                    _currentPosition.value = currentPosition.coerceAtLeast(0L)+                    _duration.value = duration.coerceAtLeast(0L)                     addListener(object : Player.Listener {                         override fun onIsPlayingChanged(playing: Boolean) {                             _isPlaying.value = playing