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 <noreply@anthropic.com>
This commit is contained in:
1 parent
bb817d8af0
commit
8695c7e7c1
3 files changed
+60
-62
No files matched your search
@@ -274,7 +274,6 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
|
||||
store,
|
||||
currentSongId,
|
||||
isPlaying,
|
||||
duration,
|
||||
queueStartVersion,
|
||||
currentTimeRef,
|
||||
hasPlayer,
|
||||
|
||||
@@ -100,6 +100,8 @@ export function useNativePlaylistSync({
|
||||
const currentTimeRef = useRef(0)
|
||||
const pendingSeekRef = useRef<number | null>(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, [])
|
||||
|
||||
@@ -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<number>
|
||||
hasPlayer: () => boolean
|
||||
@@ -43,9 +41,6 @@ export function useProgressPersistence({
|
||||
const progressRestoredForSongRef = useRef<Id<"songs"> | null>(null)
|
||||
const lastSavedProgressRef = useRef(-1)
|
||||
const prevIsPlayingRef = useRef(false)
|
||||
const [progressToRestore, setProgressToRestore] = useState<number | null>(
|
||||
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
|
||||
|
||||
Reference in new issue
Block a user