music: repeat-one is honoured when casting
- Commit
c253a4fcb4149867ee69c5fab7bd9f8638a618d7- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-30 21:30 UTC (5 days ago)
- Parents
485b0e1
While casting, the TV plays single resources and knows nothing of repeat — but CastQueue implemented only the advance-or-stop half of the queue owner's job. media3's hasNextMediaItem() disregards REPEAT_MODE_ONE, so with repeat-one set a cast track played once and the queue moved on (or parked at STATE_ENDED on the last track), while the same queue repeated correctly on the phone. The TV-ended branch now goes through an explicit pure decision: REPEAT_MODE_ONE replays (re-offering the current item — seeking the local player to it fires no transition, so the transition hook cannot do this), hasNext advances, otherwise stop; unknown modes fall through to off, matching nextRepeatMode. Repeat-all wrapping and shuffle order already travelled via the local timeline. Six new decision tests cover both repeat positions. Queue seeding, library scan, lyrics, wear sync and preview lifecycle were audited and cleared. Verified: targeted :music:testDevUnitTest passes (CastQueueEndedActionTest, CastingPlayerTest, RepeatShuffleTest); lint errors are pre-existing on untouched files; the full module suite could not complete here because a sibling session's full-suite run has held a Robolectric worker for 65+ minutes on this shared box.
Diff
music/AGENTS.md
diff --git a/music/AGENTS.md b/music/AGENTS.mdindex 7b3addb600..5efca7c87e 100644--- a/music/AGENTS.md+++ b/music/AGENTS.md@@ -21,6 +21,7 @@ _Install: ./install music (dev by default)._ **Gotchas:** - Playback state lives in the service, not the ViewModel — UI binds, never owns - Cast and local are separate player paths sharing one queue model; test handoff, not just each side+- While casting, the TV plays single resources and knows no repeat/shuffle: `CastQueue` honours the phone's repeat mode on a TV `ended` (repeat-one re-offers the same track, repeat-all wraps via the local queue, shuffle order comes from the local timeline) ## Key files ### Root — entry + navigation
music/src/main/java/com/vayunmathur/music/service/CastQueue.kt
diff --git a/music/src/main/java/com/vayunmathur/music/service/CastQueue.kt b/music/src/main/java/com/vayunmathur/music/service/CastQueue.ktindex cc06d18df8..b4a8c39bae 100644--- a/music/src/main/java/com/vayunmathur/music/service/CastQueue.kt+++ b/music/src/main/java/com/vayunmathur/music/service/CastQueue.kt@@ -150,11 +150,39 @@ class CastQueue(context: Context, private val local: ExoPlayer) { // anything was offered - a session that ended before a track loaded - moves nothing. if (offered == null) return advancedOnEnded = true- if (local.hasNextMediaItem()) {- // The transition this causes is what issues the next `PLAY_MEDIA`; nothing is sent here.- local.seekToNextMediaItem()- } else {- Log.status(TAG, "the queue is finished; the television stays where it is")+ when (endedAction(local.repeatMode, local.hasNextMediaItem())) {+ EndedAction.REPLAY ->+ // No item transition happens for the same item, so the transition hook cannot+ // re-offer it: tell the television to play it again directly.+ offer(local.currentMediaItem, startPositionMs = 0)+ EndedAction.ADVANCE ->+ // The transition this causes is what issues the next `PLAY_MEDIA`; nothing is sent here.+ local.seekToNextMediaItem()+ EndedAction.STOP ->+ Log.status(TAG, "the queue is finished; the television stays where it is")+ }+ }++ /** What an `ended` from the television should do to the phone's queue. */+ internal enum class EndedAction { REPLAY, ADVANCE, STOP }++ internal companion object {++ /**+ * The one decision behind [onTvPlayback]'s handling of a finished track.+ *+ * The television plays single resources and knows nothing of repeat, so the phone's+ * repeat mode has to be honoured here - the local player, which would repeat on its+ * own, is paused for the whole cast. [hasNext] is `Player.hasNextMediaItem`, whose+ * navigation semantics already wrap at the end under `REPEAT_MODE_ALL` and already+ * disregard `REPEAT_MODE_ONE`, which is why repeat-one must be decided before it is+ * consulted rather than inferred from it. Unknown modes fall through to the+ * off behaviour, matching `nextRepeatMode`.+ */+ internal fun endedAction(repeatMode: Int, hasNext: Boolean): EndedAction = when {+ repeatMode == Player.REPEAT_MODE_ONE -> EndedAction.REPLAY+ hasNext -> EndedAction.ADVANCE+ else -> EndedAction.STOP } }
music/src/test/java/com/vayunmathur/music/service/CastQueueEndedActionTest.kt
diff --git a/music/src/test/java/com/vayunmathur/music/service/CastQueueEndedActionTest.kt b/music/src/test/java/com/vayunmathur/music/service/CastQueueEndedActionTest.ktnew file mode 100644index 0000000000..bf5d45cee6--- /dev/null+++ b/music/src/test/java/com/vayunmathur/music/service/CastQueueEndedActionTest.kt@@ -0,0 +1,74 @@+package com.vayunmathur.music.service++import androidx.media3.common.Player+import kotlin.test.Test+import kotlin.test.assertEquals++/**+ * Pins what a finished track on the television does to the phone's queue.+ *+ * The television plays single resources and knows nothing of repeat, so the phone's+ * repeat mode has to be honoured where the `ended` snapshot is acted on. Before this+ * was decided explicitly, [CastQueue] advanced (or stopped at the end) whatever the+ * mode: with repeat-one set, a cast track played once and the queue moved on - or, on+ * the last track, simply stayed ended - while the same queue repeated correctly on+ * the phone itself.+ *+ * `hasNext` here is `Player.hasNextMediaItem`, whose navigation semantics wrap at+ * the end under repeat-all and disregard repeat-one; the cases below are stated in+ * those terms, which is why repeat-all at the last track arrives as `hasNext = true`.+ */+class CastQueueEndedActionTest {++ @Test+ fun `repeat one replays the finished track instead of advancing`() {+ assertEquals(+ CastQueue.EndedAction.REPLAY,+ CastQueue.endedAction(Player.REPEAT_MODE_ONE, hasNext = true),+ )+ }++ @Test+ fun `repeat one replays the last track instead of stopping`() {+ assertEquals(+ CastQueue.EndedAction.REPLAY,+ CastQueue.endedAction(Player.REPEAT_MODE_ONE, hasNext = false),+ )+ }++ @Test+ fun `repeat off advances while there is a next track`() {+ assertEquals(+ CastQueue.EndedAction.ADVANCE,+ CastQueue.endedAction(Player.REPEAT_MODE_OFF, hasNext = true),+ )+ }++ @Test+ fun `repeat off stops at the end of the queue`() {+ assertEquals(+ CastQueue.EndedAction.STOP,+ CastQueue.endedAction(Player.REPEAT_MODE_OFF, hasNext = false),+ )+ }++ @Test+ fun `repeat all advances, including the wrap at the end of the queue`() {+ assertEquals(+ CastQueue.EndedAction.ADVANCE,+ CastQueue.endedAction(Player.REPEAT_MODE_ALL, hasNext = true),+ )+ }++ @Test+ fun `an unknown repeat mode behaves as off`() {+ assertEquals(+ CastQueue.EndedAction.ADVANCE,+ CastQueue.endedAction(repeatMode = 99, hasNext = true),+ )+ assertEquals(+ CastQueue.EndedAction.STOP,+ CastQueue.endedAction(repeatMode = 99, hasNext = false),+ )+ }+}