youpipe: media reloads keep the live playhead instead of the stored resume point
- Commit
580eb0c73f5eb3d13ea789eec23b929d32af7603- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 08:38 UTC (5 days ago)
- Parents
39a8c98
Every media-item rebuild restarted the phone player at the
stored history position: pause at 30 s, seek to 90 s (the
position loop only persists while playing), then change
quality or audio language — the effect re-ran on its stream
keys and setMediaItem restarted at the persisted 30 s, an
instant 60 s rewind; even mid-playback it rewound to the
last history upsert. The TV player already encodes the
intended behaviour ('a later reload keeps the current
position instead of restarting the video'); the phone path
had no equivalent guard, so the effect used one start
position — stale by construction — for first build and
rebuilds alike, and CastPlayback published the same stale
value.
A pure mediaItemStartPositionMs now picks: first build of a
video -> the resume point; later builds -> the live playhead
(clamped >= 0), tracked by a per-video started state.
Regression tests pin first-build resume, rebuild-keeps-
playhead (paused-seek and mid-playback) and clamps.
Downloads, subscriptions/history attribution and extractor
mapping were audited and cleared. Verified:
:youpipe:testDevUnitTest 280 tests pass; lint errors are
pre-existing on untouched files.Diff
youpipe/src/main/java/com/vayunmathur/youpipe/ui/VideoPlayerPlaybackEffects.kt
diff --git a/youpipe/src/main/java/com/vayunmathur/youpipe/ui/VideoPlayerPlaybackEffects.kt b/youpipe/src/main/java/com/vayunmathur/youpipe/ui/VideoPlayerPlaybackEffects.ktindex c1c53adc79..9b1709b806 100644--- a/youpipe/src/main/java/com/vayunmathur/youpipe/ui/VideoPlayerPlaybackEffects.kt+++ b/youpipe/src/main/java/com/vayunmathur/youpipe/ui/VideoPlayerPlaybackEffects.kt@@ -241,6 +241,18 @@ internal fun VideoPlayerHistoryPersist( } } +/**+ * Where a (re)built media item starts. The first build for a video starts at the+ * stored resume point ([resumePositionMs]); every later rebuild (quality or+ * language change re-runs the media-item effect) keeps the live playhead+ * ([currentPositionMs]) instead. Pure so it is unit-testable.+ */+internal fun mediaItemStartPositionMs(+ started: Boolean,+ resumePositionMs: Long,+ currentPositionMs: Long,+): Long = if (started) currentPositionMs.coerceAtLeast(0L) else resumePositionMs.coerceAtLeast(0L)+ /** Builds the media item (audio URI in metadata extras, subtitle configs) and prepares it. */ @Composable internal fun VideoPlayerMediaItemEffect(@@ -250,6 +262,7 @@ internal fun VideoPlayerMediaItemEffect( videoInfo: VideoInfo, subtitles: List<SubtitleTrack>, timeWatched: Long,+ startedState: MutableState<Boolean>, ) { // 3. Updated LaunchedEffect to pass audio URI through Metadata Extras LaunchedEffect(controller, currentVideoStream, currentAudioStream, videoInfo.name, videoInfo.author, subtitles) {@@ -288,9 +301,17 @@ internal fun VideoPlayerMediaItemEffect( .setMediaMetadata(metadataBuilder.build()) .build() - player.setMediaItem(mediaItem, timeWatched)+ // Only the first build may start at the stored resume point: timeWatched is+ // the persisted history progress, which lags the live playhead (history is+ // written every 5 s and only while playing), so reusing it on a quality or+ // language rebuild seeks backwards — furthest after a seek made while paused,+ // which is never persisted until playback resumes. The TV media-item effect+ // keeps the current position on reloads for exactly this reason.+ val startAt = mediaItemStartPositionMs(startedState.value, timeWatched, player.currentPosition)+ startedState.value = true+ player.setMediaItem(mediaItem, startAt) player.prepare()- CastPlayback.update { it.copy(positionMs = timeWatched) }+ CastPlayback.update { it.copy(positionMs = startAt) } } }
youpipe/src/main/java/com/vayunmathur/youpipe/ui/VideoPlayerPlaybackState.kt
diff --git a/youpipe/src/main/java/com/vayunmathur/youpipe/ui/VideoPlayerPlaybackState.kt b/youpipe/src/main/java/com/vayunmathur/youpipe/ui/VideoPlayerPlaybackState.ktindex c122846db3..8b8a25c51d 100644--- a/youpipe/src/main/java/com/vayunmathur/youpipe/ui/VideoPlayerPlaybackState.kt+++ b/youpipe/src/main/java/com/vayunmathur/youpipe/ui/VideoPlayerPlaybackState.kt@@ -227,7 +227,15 @@ internal fun rememberVideoPlayerPlaybackState( val historyVideo by historyFlow.collectAsState(initial = null) val timeWatched = historyVideo?.progress ?: 0 - VideoPlayerMediaItemEffect(controller, currentVideoStream, currentAudioStream, videoInfo, subtitles, timeWatched)+ // Whether the media item for this video has been built once: the first build+ // resumes from history, later rebuilds (quality/language) keep the playhead.+ // Keyed on the video so navigating to another video resumes it afresh.+ val mediaItemStartedState = remember(videoInfo.videoID) { mutableStateOf(false) }++ VideoPlayerMediaItemEffect(+ controller, currentVideoStream, currentAudioStream, videoInfo, subtitles, timeWatched,+ mediaItemStartedState,+ ) VideoPlayerSubtitleEffect(controller, selectedSubtitle) VideoPlayerTempoEffect(controller, playbackSpeed, playbackPitch)
youpipe/src/test/java/com/vayunmathur/youpipe/ui/MediaItemStartPositionTest.kt
diff --git a/youpipe/src/test/java/com/vayunmathur/youpipe/ui/MediaItemStartPositionTest.kt b/youpipe/src/test/java/com/vayunmathur/youpipe/ui/MediaItemStartPositionTest.ktnew file mode 100644index 0000000000..1a88d6dc57--- /dev/null+++ b/youpipe/src/test/java/com/vayunmathur/youpipe/ui/MediaItemStartPositionTest.kt@@ -0,0 +1,51 @@+package com.vayunmathur.youpipe.ui++import kotlin.test.Test+import kotlin.test.assertEquals++/**+ * Regression test for the phone player's media-item rebuilds: changing quality+ * or audio language re-runs the media-item effect, and the pre-fix effect+ * restarted playback at the stored history progress (`timeWatched`). That+ * value lags the live playhead (history is upserted every 5 s and only while+ * playing), so a rebuild seeked backwards — furthest after a seek made while+ * paused, which is never persisted until playback resumes. The first build+ * must still resume from history, and later builds must keep the playhead,+ * matching `TvPlayerMediaItemEffect`.+ */+class MediaItemStartPositionTest {++ @Test+ fun `first build starts at the stored resume point`() {+ assertEquals(+ 30_000L,+ mediaItemStartPositionMs(started = false, resumePositionMs = 30_000L, currentPositionMs = 0L),+ )+ }++ @Test+ fun `rebuild keeps the live playhead instead of the stale resume point`() {+ // Paused seek to 90 s with history still at 30 s, then a quality change.+ assertEquals(+ 90_000L,+ mediaItemStartPositionMs(started = true, resumePositionMs = 30_000L, currentPositionMs = 90_000L),+ )+ // Mid-playback rebuild, playhead between history upserts.+ assertEquals(+ 47_300L,+ mediaItemStartPositionMs(started = true, resumePositionMs = 45_000L, currentPositionMs = 47_300L),+ )+ }++ @Test+ fun `negative positions clamp to zero`() {+ assertEquals(+ 0L,+ mediaItemStartPositionMs(started = false, resumePositionMs = -1L, currentPositionMs = 5_000L),+ )+ assertEquals(+ 0L,+ mediaItemStartPositionMs(started = true, resumePositionMs = 5_000L, currentPositionMs = -1L),+ )+ }+}