From 8695c7e7c14bac11abfa1599df1da00366a5a067 Mon Sep 17 00:00:00 2001 From: Joshua Higgins Date: Mon, 16 Nov 2026 20:35:19 -0500 Subject: [PATCH] fix(mobile): start restored playback at the saved position The restore seek was queued only after the player loaded, so playback requested before then (Play right after launch, a Siri resume) started at 0:00 and jumped. Queue the seek when the song becomes current, and hold off starting the player until a pending seek lands. Co-Authored-By: Claude Opus 5.5 --- apps/mobile/contexts/playback.tsx | 1 - apps/mobile/hooks/use-native-playlist-sync.ts | 85 +++++++++++-------- apps/mobile/hooks/use-progress-persistence.ts | 36 ++------ 3 files changed, 60 insertions(+), 62 deletions(-) diff --git a/apps/mobile/contexts/playback.tsx b/apps/mobile/contexts/playback.tsx index dab83be..a95720a 100644 --- a/apps/mobile/contexts/playback.tsx +++ b/apps/mobile/contexts/playback.tsx @@ -274,7 +274,6 @@ export function PlaybackProvider({ children }: { children: ReactNode }) { store, currentSongId, isPlaying, - duration, queueStartVersion, currentTimeRef, hasPlayer, diff --git a/apps/mobile/hooks/use-native-playlist-sync.ts b/apps/mobile/hooks/use-native-playlist-sync.ts index f2fa1bf..03a5b37 100644 --- a/apps/mobile/hooks/use-native-playlist-sync.ts +++ b/apps/mobile/hooks/use-native-playlist-sync.ts @@ -100,6 +100,8 @@ export function useNativePlaylistSync({ const currentTimeRef = useRef(0) const pendingSeekRef = useRef(null) const seekInFlightRef = useRef(false) + // Set while `play()` waits for a pending seek to land. + const deferredPlayRef = useRef(false) const loadRequestIdRef = useRef(0) const [isPlaying, setIsPlayingState] = useState(false) @@ -180,6 +182,7 @@ export function useNativePlaylistSync({ const resetPosition = useCallback(() => { pendingSeekRef.current = null + deferredPlayRef.current = false currentTimeRef.current = 0 setCurrentTime(0) setDuration(0) @@ -193,6 +196,19 @@ export function useNativePlaylistSync({ resetPosition() }, [destroyPlaylist, resetPosition]) + /** + * Starts the player, unless a seek (a restored position) is still pending. + * Then the status handler starts it once the seek lands, so playback never + * starts at 0:00 and jumps. + */ + const startPlayer = useCallback((playlist: AudioPlaylist) => { + if (pendingSeekRef.current !== null && pendingSeekRef.current > 0) { + deferredPlayRef.current = true + return + } + playlist.play() + }, []) + const handlePlaylistStatusUpdate = useCallback( (status: AudioPlaylistStatus) => { settleWaiters() @@ -207,7 +223,10 @@ export function useNativePlaylistSync({ } if (pendingSeekRef.current !== null && pendingSeekRef.current > 0) { - const seekSeconds = pendingSeekRef.current + const seekSeconds = + status.duration > 0 + ? Math.min(pendingSeekRef.current, status.duration) + : pendingSeekRef.current pendingSeekRef.current = null const playlist = playlistRef.current if (playlist) { @@ -215,6 +234,10 @@ export function useNativePlaylistSync({ seekInFlightRef.current = true void playlist.seekTo(seekSeconds).finally(() => { seekInFlightRef.current = false + if (deferredPlayRef.current && playlistRef.current === playlist) { + deferredPlayRef.current = false + playlist.play() + } }) } currentTimeRef.current = seekSeconds @@ -230,7 +253,11 @@ export function useNativePlaylistSync({ isPlayingRef.current = true setIsPlayingState(true) } - } else if (isPlayingRef.current && !status.didJustFinish) { + } else if ( + isPlayingRef.current && + !status.didJustFinish && + !deferredPlayRef.current + ) { autoPlayNextRef.current = false isPlayingRef.current = false setIsPlayingState(false) @@ -363,17 +390,21 @@ export function useNativePlaylistSync({ } }, [repeatEnabled]) - const setIsPlaying = useCallback((playing: boolean) => { - isPlayingRef.current = playing - setIsPlayingState(playing) - const playlist = playlistRef.current - if (!playlist) return - if (playing) { - playlist.play() - } else { - playlist.pause() - } - }, []) + const setIsPlaying = useCallback( + (playing: boolean) => { + isPlayingRef.current = playing + setIsPlayingState(playing) + const playlist = playlistRef.current + if (!playlist) return + if (playing) { + startPlayer(playlist) + } else { + deferredPlayRef.current = false + playlist.pause() + } + }, + [startPlayer] + ) const seekTo = useCallback( async (seconds: number) => { @@ -452,7 +483,7 @@ export function useNativePlaylistSync({ if (shouldPlay) { isPlayingRef.current = true setIsPlayingState(true) - playlist.play() + startPlayer(playlist) } return } @@ -519,7 +550,7 @@ export function useNativePlaylistSync({ if (shouldPlay && !loadedPlaylist.playing) { isPlayingRef.current = true setIsPlayingState(true) - loadedPlaylist.play() + startPlayer(loadedPlaylist) } return } @@ -562,7 +593,7 @@ export function useNativePlaylistSync({ if (shouldPlay) { isPlayingRef.current = true setIsPlayingState(true) - playlist.play() + startPlayer(playlist) } else { playlist.pause() // Enable audio for the restored queue without starting playback. @@ -600,6 +631,7 @@ export function useNativePlaylistSync({ queueSyncKey, queueStartVersion, repeatEnabled, + startPlayer, userPlaybackKey, ]) @@ -657,28 +689,13 @@ export function useNativePlaylistSync({ ) /** - * Moves the current song to `seconds` as soon as it is loaded. A paused - * player sends no further status updates once loaded, so seek right away - * when it already is, and show the position either way. + * Starts the current song at `seconds` once it loads, and shows that + * position right away. Playback waits for the seek (see `startPlayer`). */ const queueSeek = useCallback((seconds: number) => { + pendingSeekRef.current = seconds currentTimeRef.current = seconds setCurrentTime(seconds) - const playlist = playlistRef.current - const songId = storeRef.current.getState().queue[0] - if ( - playlist?.isLoaded && - loadedSongIdsRef.current[playlist.currentIndex] === songId - ) { - pendingSeekRef.current = null - // Statuses report the old position until the seek lands. - seekInFlightRef.current = true - void playlist.seekTo(seconds).finally(() => { - seekInFlightRef.current = false - }) - return - } - pendingSeekRef.current = seconds }, []) const hasPlayer = useCallback(() => playlistRef.current !== null, []) diff --git a/apps/mobile/hooks/use-progress-persistence.ts b/apps/mobile/hooks/use-progress-persistence.ts index 906e1c6..6b4bde9 100644 --- a/apps/mobile/hooks/use-progress-persistence.ts +++ b/apps/mobile/hooks/use-progress-persistence.ts @@ -1,5 +1,5 @@ import { useMutation } from "convex/react" -import { useCallback, useEffect, useRef, useState, type RefObject } from "react" +import { useCallback, useEffect, useRef, type RefObject } from "react" import { AppState } from "react-native" import { api } from "@otunes/convex/_generated/api" @@ -17,7 +17,6 @@ export function useProgressPersistence({ store, currentSongId, isPlaying, - duration, queueStartVersion, currentTimeRef, hasPlayer, @@ -27,7 +26,6 @@ export function useProgressPersistence({ store: PlaybackStore currentSongId: Id<"songs"> | null isPlaying: boolean - duration: number queueStartVersion: number currentTimeRef: RefObject hasPlayer: () => boolean @@ -43,9 +41,6 @@ export function useProgressPersistence({ const progressRestoredForSongRef = useRef | null>(null) const lastSavedProgressRef = useRef(-1) const prevIsPlayingRef = useRef(false) - const [progressToRestore, setProgressToRestore] = useState( - null - ) const saveProgress = useCallback( (seconds: number) => { @@ -66,7 +61,6 @@ export function useProgressPersistence({ const resetProgress = useCallback(() => { progressRestoredForSongRef.current = null lastSavedProgressRef.current = -1 - setProgressToRestore(null) }, []) useEffect(() => { @@ -79,17 +73,12 @@ export function useProgressPersistence({ progressRestoredForSongRef.current = currentSongId lastSavedProgressRef.current = -1 resetPosition() - setProgressToRestore(store.getState().progressSeconds) - }, [currentSongId, queueStartVersion, resetPosition, store]) - - useEffect(() => { - if (progressToRestore === null || progressToRestore <= 0) return - if (!currentSongId || !hasPlayer()) return - - const maxDuration = duration > 0 ? duration : progressToRestore - queueSeek(Math.min(progressToRestore, maxDuration)) - setProgressToRestore(null) - }, [progressToRestore, currentSongId, duration, hasPlayer, queueSeek]) + // Queue the seek before the song loads, so playback starts there. + const progressSeconds = store.getState().progressSeconds + if (progressSeconds > 0) { + queueSeek(progressSeconds) + } + }, [currentSongId, queueStartVersion, queueSeek, resetPosition, store]) useEffect(() => { if (!currentSongId) return @@ -101,20 +90,13 @@ export function useProgressPersistence({ useEffect(() => { const subscription = AppState.addEventListener("change", (state) => { if (state === "active") return - const restorePending = progressToRestore !== null && progressToRestore > 0 - if (currentSongId && hasPlayer() && !restorePending) { + if (currentSongId && hasPlayer()) { saveProgress(currentTimeRef.current) } flushPersistentCache() }) return () => subscription.remove() - }, [ - currentSongId, - currentTimeRef, - hasPlayer, - progressToRestore, - saveProgress, - ]) + }, [currentSongId, currentTimeRef, hasPlayer, saveProgress]) useEffect(() => { if (!isPlaying || !currentSongId) return