From cc9654c7471939c2f062a2020a397eb4d442e770 Mon Sep 17 00:00:00 2001 From: Joshua Higgins Date: Mon, 16 Nov 2026 13:54:07 -0500 Subject: [PATCH 1/3] refactor(mobile): split playback provider and replace polling with player events Move native playlist sync and progress persistence into their own hooks, wait on playlist events instead of timers and polling, and build the album/artist/playlist play actions from one table. Co-Authored-By: Claude Opus 5.5 --- apps/mobile/contexts/playback.tsx | 928 +++--------------- apps/mobile/hooks/use-native-playlist-sync.ts | 683 +++++++++++++ apps/mobile/hooks/use-progress-persistence.ts | 128 +++ 3 files changed, 955 insertions(+), 784 deletions(-) create mode 100644 apps/mobile/hooks/use-native-playlist-sync.ts create mode 100644 apps/mobile/hooks/use-progress-persistence.ts diff --git a/apps/mobile/contexts/playback.tsx b/apps/mobile/contexts/playback.tsx index 96241e8..dab83be 100644 --- a/apps/mobile/contexts/playback.tsx +++ b/apps/mobile/contexts/playback.tsx @@ -1,13 +1,3 @@ -import { dolbyAtmosSupported } from "@/lib/playback-capabilities" -import { - createAudioPlaylist, - setAudioModeAsync, - setIsAudioActiveAsync, - type AudioMetadata, - type AudioPlaylist, - type AudioPlaylistStatus, - type AudioSource, -} from "expo-audio" import { useConvex, useMutation, useQuery } from "convex/react" import { createContext, @@ -20,57 +10,27 @@ import { useSyncExternalStore, type ReactNode, } from "react" -import { AppState } from "react-native" import { ANONYMOUS_CACHE_SCOPE, useCacheScope, } from "@/contexts/persistent-cache" -import { useServerConfig } from "@/contexts/server-config" -import { useCachedQuery } from "@/hooks/use-cached-query" +import { useNativePlaylistSync } from "@/hooks/use-native-playlist-sync" +import { useProgressPersistence } from "@/hooks/use-progress-persistence" import { api } from "@otunes/convex/_generated/api" import type { Id } from "@otunes/convex/_generated/dataModel" import type { BrowseSong } from "@otunes/convex/types" -import { playbackStreamPlan } from "@otunes/convex/playback-stream" import { fetchCachedQuery } from "@otunes/convex/client/cached-query" import * as playbackQueue from "@otunes/convex/client/playback-queue" import type { RepeatMode } from "@otunes/convex/client/playback-queue" import { getPlaybackStore } from "@otunes/convex/client/playback-store" -import { getAuthCookieHeader } from "@/lib/auth-cookies" -import { flushPersistentCache } from "@/lib/persistent-cache" -import { loadPlaybackSongs } from "@/lib/playback-files" import { rememberSongs, useSong, useSongs } from "@/lib/song-cache" import { donateMusicPlay, type MusicEntityKind } from "@/lib/app-intents" import { showError } from "@/lib/toast" -import { albumCoverUrl, songAudioSource } from "@/lib/urls" -import { - activatePlaylistLockScreen, - releaseAudioPlaylist, - updatePlaylistLockScreenMetadata, -} from "@/lib/audio-playlist-lock-screen" -import { - nativePlaylistEdits, - nativePlaylistNeedsRebuild, - nativePlaylistSongIds, - repeatToPlaylistLoop, -} from "@/lib/native-playlist-window" const PREVIOUS_TRACK_RESTART_THRESHOLD_SECONDS = 5 -const PROGRESS_SAVE_INTERVAL_MS = 5000 const PREFETCH_UPCOMING_SONGS = 5 -function lockScreenMetadata( - song: BrowseSong, - serverUrl: string -): AudioMetadata { - return { - title: song.title, - artist: song.artist.name, - albumTitle: song.album.title, - artworkUrl: albumCoverUrl(serverUrl, song.album.id, 512), - } -} - type PlaybackContextValue = { queueSongIds: Id<"songs">[] queueHistorySongIds: Id<"songs">[] @@ -176,6 +136,27 @@ type PlaybackQueueValue = Pick< | "repeatEnabled" > +type CollectionPlayers = Pick< + PlaybackActionsValue, + | "playAlbum" + | "playAlbumShuffled" + | "playArtist" + | "playArtistShuffled" + | "playPlaylist" + | "playPlaylistShuffled" +> + +type RecordedPlay = { + playlistId?: Id<"playlists"> + origin?: string + entity?: { kind: MusicEntityKind; id: string } +} + +type CollectionPlay = { + songIds: Id<"songs">[] | Promise[]> + recorded: RecordedPlay +} + const PlaybackContext = createContext(null) const PlaybackActionsContext = createContext(null) const PlaybackQueueContext = createContext(null) @@ -206,26 +187,19 @@ export function usePlaybackQueue() { export function PlaybackProvider({ children }: { children: ReactNode }) { const convex = useConvex() - const { config } = useServerConfig() const cacheScope = useCacheScope() const isSignedIn = cacheScope !== ANONYMOUS_CACHE_SCOPE const store = useMemo( () => getPlaybackStore(isSignedIn ? cacheScope : null), [cacheScope, isSignedIn] ) - const storeRef = useRef(store) - storeRef.current = store const playback = useSyncExternalStore(store.subscribe, store.getSnapshot) - const user = useCachedQuery(api.auth.currentUser) const serverPlayback = useQuery( api.playback.getPlaybackState, isSignedIn ? {} : "skip" ) const syncPlaybackStateMutation = useMutation(api.playback.syncPlaybackState) const recordPlayMutation = useMutation(api.playback.recordPlay) - const updateSongProgressMutation = useMutation( - api.playback.updateSongProgress - ) useEffect(() => { if (!isSignedIn) return @@ -256,36 +230,9 @@ export function PlaybackProvider({ children }: { children: ReactNode }) { [recordPlayMutation] ) - const playlistRef = useRef(null) - const statusSubscriptionRef = useRef<{ remove: () => void } | null>(null) - const trackChangedSubscriptionRef = useRef<{ remove: () => void } | null>( - null - ) - const loadedSongIdsRef = useRef[]>([]) - const loadedLoadKeyRef = useRef(null) - const ignoreTrackChangeRef = useRef(false) - const autoPlayNextRef = useRef(false) - const isPlayingRef = useRef(false) - const progressRestoredForSongRef = useRef | null>(null) - const lastSavedProgressRef = useRef(-1) - const prevIsPlayingRef = useRef(false) - const currentTimeRef = useRef(0) - const pendingSeekRef = useRef(null) - const seekInFlightRef = useRef(false) - const loadRequestIdRef = useRef(0) const queueOriginRef = useRef(undefined) const playRequestRef = useRef(0) - const ignoreTrackChangeTimerRef = useRef | null>(null) - const [queueStartVersion, setQueueStartVersion] = useState(0) - const [isPlaying, setIsPlayingState] = useState(false) - const [currentTime, setCurrentTime] = useState(0) - const [measuredDuration, setDuration] = useState(0) - const [progressToRestore, setProgressToRestore] = useState( - null - ) const queueSongIds = playback.queue const queueHistorySongIds = playback.history @@ -300,564 +247,53 @@ export function PlaybackProvider({ children }: { children: ReactNode }) { [queueHistorySongIds, queueSongIds] ) useSongs(adjacentSongIds) - const duration = - measuredDuration > 0 - ? measuredDuration - : (currentSong?.durationSeconds ?? 0) - const queueSyncKey = useMemo( - () => JSON.stringify(queueSongIds), - [queueSongIds] - ) - const historySyncKey = useMemo( - () => JSON.stringify(queueHistorySongIds), - [queueHistorySongIds] - ) - const currentSongSyncKey = currentSong - ? JSON.stringify([ - currentSong.id, - currentSong.title, - currentSong.artist.name, - currentSong.album.id, - currentSong.album.title, - currentSong.album.cover ?? "", - ]) - : "" - const userPlaybackKey = user - ? JSON.stringify([ - user._id, - user.playbackQuality, - dolbyAtmosSupported && (user.preferAtmos ?? false), - ]) - : "" - const scheduleTrackChangeHandling = useCallback(() => { - if (ignoreTrackChangeTimerRef.current) { - clearTimeout(ignoreTrackChangeTimerRef.current) - } - ignoreTrackChangeTimerRef.current = setTimeout(() => { - ignoreTrackChangeTimerRef.current = null - ignoreTrackChangeRef.current = false - }, 150) - }, []) - - const destroyPlaylist = useCallback(() => { - if (ignoreTrackChangeTimerRef.current) { - clearTimeout(ignoreTrackChangeTimerRef.current) - ignoreTrackChangeTimerRef.current = null - } - statusSubscriptionRef.current?.remove() - statusSubscriptionRef.current = null - trackChangedSubscriptionRef.current?.remove() - trackChangedSubscriptionRef.current = null - const playlist = playlistRef.current - if (playlist) { - playlist.pause() - releaseAudioPlaylist(playlist) - } - playlistRef.current = null - loadedSongIdsRef.current = [] - loadedLoadKeyRef.current = null - }, []) - - // A new queue must not inherit the native player's position or a pending seek. - const resetQueuePlayback = useCallback(() => { - loadRequestIdRef.current += 1 - destroyPlaylist() - pendingSeekRef.current = null - seekInFlightRef.current = false - progressRestoredForSongRef.current = null - lastSavedProgressRef.current = -1 - currentTimeRef.current = 0 - setCurrentTime(0) - setDuration(0) - setProgressToRestore(null) - setQueueStartVersion((version) => version + 1) - }, [destroyPlaylist]) - - const handlePlaylistStatusUpdate = useCallback( - (status: AudioPlaylistStatus) => { - if (!status.isLoaded) return - const state = storeRef.current.getState() - if (loadedSongIdsRef.current[status.currentIndex] !== state.queue[0]) { - return - } - - if (status.duration > 0) { - setDuration(status.duration) - } - - if (pendingSeekRef.current !== null && pendingSeekRef.current > 0) { - const seekSeconds = pendingSeekRef.current - pendingSeekRef.current = null - const playlist = playlistRef.current - if (playlist) { - // Statuses report the old position until the seek lands. - seekInFlightRef.current = true - void playlist.seekTo(seekSeconds).finally(() => { - seekInFlightRef.current = false - }) - } - currentTimeRef.current = seekSeconds - setCurrentTime(seekSeconds) - } else if (!seekInFlightRef.current) { - currentTimeRef.current = status.currentTime - setCurrentTime(status.currentTime) - } - - if (status.playing) { - autoPlayNextRef.current = false - if (!isPlayingRef.current) { - isPlayingRef.current = true - setIsPlayingState(true) - } - } else if (isPlayingRef.current && !status.didJustFinish) { - autoPlayNextRef.current = false - isPlayingRef.current = false - setIsPlayingState(false) - } - - if (!status.didJustFinish) return - if (state.repeatEnabled === "once") return - if (status.currentIndex < status.trackCount - 1) return - - const shouldContinue = - state.repeatEnabled !== "none" || state.queue.length > 1 - if (!shouldContinue) { - autoPlayNextRef.current = false - isPlayingRef.current = false - setIsPlayingState(false) - storeRef.current.update(playbackQueue.skipNext) - void playlistRef.current?.seekTo(0) - currentTimeRef.current = 0 - setCurrentTime(0) - return - } - - autoPlayNextRef.current = true - isPlayingRef.current = true - setIsPlayingState(true) - storeRef.current.update(playbackQueue.skipNext) - if (storeRef.current.getState().queue[0] === state.queue[0]) { - const playlist = playlistRef.current - void playlist?.seekTo(0) - playlist?.play() - } - }, - [] - ) - - const handleTrackChanged = useCallback( - (data: { currentIndex: number; previousIndex: number }) => { - if (ignoreTrackChangeRef.current) return - // The window is history then queue, so the current track sits at - // `history.length`. Android's `previousIndex` goes stale when tracks - // are inserted or removed before it. - const steps = - data.currentIndex - storeRef.current.getState().history.length - if (steps === 0) return - - autoPlayNextRef.current = true - isPlayingRef.current = true - setIsPlayingState(true) - - storeRef.current.update((state) => { - if (steps === 1) return playbackQueue.skipNext(state) - if (steps > 1) return playbackQueue.skipToQueueIndex(state, steps) - let next = state - for (let i = 0; i < -steps; i++) { - next = playbackQueue.skipPrevious(next) - } - return next - }) - }, - [] - ) - - const ensurePlaylist = useCallback( - (sources: AudioSource[]) => { - if (playlistRef.current) { - return playlistRef.current - } - - ignoreTrackChangeRef.current = true - const playlist = createAudioPlaylist({ - sources, - updateInterval: 500, - loop: repeatToPlaylistLoop(storeRef.current.getState().repeatEnabled), - }) - // System volume controls loudness; keep the player gain at full scale. - playlist.volume = 1 - statusSubscriptionRef.current = playlist.addListener( - "playlistStatusUpdate", - handlePlaylistStatusUpdate - ) - trackChangedSubscriptionRef.current = playlist.addListener( - "trackChanged", - handleTrackChanged - ) - playlistRef.current = playlist - scheduleTrackChangeHandling() - return playlist - }, - [ - handlePlaylistStatusUpdate, - handleTrackChanged, - scheduleTrackChangeHandling, - ] - ) - - useEffect(() => { - let cancelled = false - void setAudioModeAsync({ - playsInSilentMode: true, - shouldPlayInBackground: true, - interruptionMode: "doNotMix", - }).catch(() => { - if (!cancelled) { - showError("Audio playback could not be initialized.") - } - }) - - return () => { - cancelled = true - destroyPlaylist() - } - }, [destroyPlaylist]) - - useEffect(() => { - const playlist = playlistRef.current - if (playlist) { - playlist.loop = repeatToPlaylistLoop(repeatEnabled) - } - }, [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 seekTo = useCallback( - async (seconds: number) => { - const playlist = playlistRef.current - if (!playlist) return - const maxDuration = playlist.duration || duration || 0 - const clamped = Math.max(0, Math.min(seconds, maxDuration)) - await playlist.seekTo(clamped) - currentTimeRef.current = clamped - setCurrentTime(clamped) - }, - [duration] - ) - - const saveProgress = useCallback( - (seconds: number) => { - const songId = storeRef.current.getState().queue[0] - if (!songId) return - const progressSeconds = Math.max(0, Math.floor(seconds)) - if (progressSeconds === lastSavedProgressRef.current) return - lastSavedProgressRef.current = progressSeconds - storeRef.current.setProgress(songId, progressSeconds) - void updateSongProgressMutation({ - songId, - progressSeconds, - }).catch(() => {}) - }, - [updateSongProgressMutation] - ) - - const handlePlaybackLoadFailure = useCallback(() => { - autoPlayNextRef.current = false - isPlayingRef.current = false - destroyPlaylist() - setIsPlayingState(false) - showError("Could not play this track. The queue has been cleared.") - storeRef.current.update(playbackQueue.clearQueue) - }, [destroyPlaylist]) - - useEffect(() => { - if (!currentSongId) { - progressRestoredForSongRef.current = null - destroyPlaylist() - return - } - - if (progressRestoredForSongRef.current === currentSongId) return - progressRestoredForSongRef.current = currentSongId - lastSavedProgressRef.current = -1 - pendingSeekRef.current = null - currentTimeRef.current = 0 - setCurrentTime(0) - setDuration(0) - setProgressToRestore(store.getState().progressSeconds) - }, [currentSongId, destroyPlaylist, queueStartVersion, store]) - - useEffect(() => { - if (progressToRestore === null || progressToRestore <= 0) return - if (!currentSongId || !playlistRef.current) return - - const maxDuration = duration > 0 ? duration : progressToRestore - const clamped = Math.min(progressToRestore, maxDuration) - pendingSeekRef.current = clamped - setProgressToRestore(null) - }, [progressToRestore, currentSongId, duration]) - - useEffect(() => { - if (!currentSongId) return - if (prevIsPlayingRef.current === isPlaying) return - prevIsPlayingRef.current = isPlaying - saveProgress(currentTimeRef.current) - }, [isPlaying, currentSongId, saveProgress]) - - useEffect(() => { - const subscription = AppState.addEventListener("change", (state) => { - if (state === "active") return - const restorePending = progressToRestore !== null && progressToRestore > 0 - if (currentSongId && playlistRef.current && !restorePending) { - saveProgress(currentTimeRef.current) - } - flushPersistentCache() - }) - return () => subscription.remove() - }, [currentSongId, progressToRestore, saveProgress]) - - useEffect(() => { - const loadRequestId = ++loadRequestIdRef.current - const isCurrentLoad = () => loadRequestIdRef.current === loadRequestId - - async function syncPlaylist() { - if (!config || !currentSongId || !user) { - if (!currentSongId) { - destroyPlaylist() - } - return - } - - const windowSongIds = nativePlaylistSongIds( - queueHistorySongIds, - queueSongIds - ) - const currentIndex = queueHistorySongIds.length - const cookieHeader = await getAuthCookieHeader( - config.serverUrl, - config.convexUrl - ) - if (!cookieHeader) { - showError("Your session has expired. Please sign in again.") - return - } - if (!isCurrentLoad()) return - - const loadKey = `${user.playbackQuality}:${dolbyAtmosSupported && (user.preferAtmos ?? false)}:${cookieHeader}` - const needsRebuild = - loadedLoadKeyRef.current !== loadKey || - nativePlaylistNeedsRebuild( - loadedSongIdsRef.current, - queueHistorySongIds, - queueSongIds - ) - - const metadata = currentSong - ? lockScreenMetadata(currentSong, config.serverUrl) - : null - const shouldPlay = isPlayingRef.current || autoPlayNextRef.current - - if (!needsRebuild) { - const playlist = playlistRef.current - if (!playlist) return - ignoreTrackChangeRef.current = true - if (playlist.currentIndex !== currentIndex) { - playlist.skipTo(currentIndex) - } - playlist.loop = repeatToPlaylistLoop(repeatEnabled) - if (metadata) { - updatePlaylistLockScreenMetadata(playlist, metadata) - } - scheduleTrackChangeHandling() - if (shouldPlay) { - isPlayingRef.current = true - setIsPlayingState(true) - playlist.play() - } - return - } - - const buildSources = async (songIds: Id<"songs">[]) => { - const songs = await loadPlaybackSongs(convex, cacheScope, songIds) - const sources = new Map, AudioSource>() - for (const song of songs) { - if (!song) return null - const plan = playbackStreamPlan( - song.files, - user.playbackQuality, - dolbyAtmosSupported && (user.preferAtmos ?? false) - ) - sources.set( - song.id, - songAudioSource( - config.serverUrl, - song.id, - plan.playbackQuality, - plan.preferAtmos, - plan.useHls, - { cookieHeader, songTitle: song.title } - ) - ) - } - return sources - } - - // Queue and history edits around the playing track are applied in place - // so playback isn't interrupted. - const loadedPlaylist = playlistRef.current - const edits = - loadedPlaylist && loadedLoadKeyRef.current === loadKey - ? nativePlaylistEdits( - loadedSongIdsRef.current, - loadedPlaylist.currentIndex, - windowSongIds, - currentIndex - ) - : null - if (loadedPlaylist && edits) { - const addedSongIds = edits.flatMap((edit) => - edit.type === "remove" ? [] : [edit.songId] - ) - const sources = await buildSources([...new Set(addedSongIds)]) - if (!isCurrentLoad()) return - if (!sources) { - handlePlaybackLoadFailure() - return - } - ignoreTrackChangeRef.current = true - for (const edit of edits) { - if (edit.type === "remove") { - loadedPlaylist.remove(edit.index) - } else if (edit.type === "insert") { - loadedPlaylist.insert(sources.get(edit.songId)!, edit.index) - } else { - loadedPlaylist.add(sources.get(edit.songId)!) - } - } - loadedSongIdsRef.current = windowSongIds - loadedPlaylist.loop = repeatToPlaylistLoop(repeatEnabled) - scheduleTrackChangeHandling() - if (shouldPlay && !loadedPlaylist.playing) { - isPlayingRef.current = true - setIsPlayingState(true) - loadedPlaylist.play() - } - return - } - - const sourcesById = await buildSources(windowSongIds) - if (!isCurrentLoad()) return - if (!sourcesById) { - handlePlaybackLoadFailure() - return - } - const sources = windowSongIds.map((songId) => sourcesById.get(songId)!) - - // Keep the position when only the stream settings changed. - const resumeSeconds = - loadedPlaylist && - loadedSongIdsRef.current[loadedPlaylist.currentIndex] === currentSongId - ? currentTimeRef.current - : null - - ignoreTrackChangeRef.current = true - destroyPlaylist() - if (!isCurrentLoad()) { - ignoreTrackChangeRef.current = false - return - } - - if (resumeSeconds !== null && resumeSeconds > 0) { - pendingSeekRef.current = resumeSeconds - } - // Start at the current track and put history in front of it, so an - // earlier song is never loaded first. - const playlist = ensurePlaylist(sources.slice(currentIndex)) - for (let index = 0; index < currentIndex; index++) { - playlist.insert(sources[index], index) - } - loadedSongIdsRef.current = windowSongIds - loadedLoadKeyRef.current = loadKey - playlist.loop = repeatToPlaylistLoop(repeatEnabled) - activatePlaylistLockScreen( - playlist, - metadata ?? { title: currentSong?.title } - ) - scheduleTrackChangeHandling() - - if (shouldPlay) { - isPlayingRef.current = true - setIsPlayingState(true) - playlist.play() - } else { - playlist.pause() - // Enable audio for the restored queue without starting playback. - try { - await setIsAudioActiveAsync(true) - } catch { - if (isCurrentLoad()) { - showError("Audio playback could not be initialized.") - } - } - } - } - - void syncPlaylist().catch(() => { - if (!isCurrentLoad()) return - handlePlaybackLoadFailure() - }) - - return () => { - if (loadRequestIdRef.current === loadRequestId) { - loadRequestIdRef.current += 1 - } - } - }, [ - cacheScope, - config, - convex, - currentSongId, - currentSongSyncKey, - destroyPlaylist, - ensurePlaylist, - handlePlaybackLoadFailure, - historySyncKey, - queueSyncKey, - queueStartVersion, + const { + isPlaying, + setIsPlaying, + currentTime, + currentTimeRef, + duration, + seekTo, + queueSeek, + hasPlayer, + requestAutoPlay, + resetPosition, + resetForNewQueue, + waitForSong, + } = useNativePlaylistSync({ + store, + queueSongIds, + queueHistorySongIds, repeatEnabled, - scheduleTrackChangeHandling, - userPlaybackKey, - ]) + currentSong, + queueStartVersion, + }) - useEffect(() => { - if (!isPlaying || !currentSongId) return - const interval = setInterval(() => { - saveProgress(currentTimeRef.current) - }, PROGRESS_SAVE_INTERVAL_MS) - return () => clearInterval(interval) - }, [isPlaying, currentSongId, saveProgress]) + const { resetProgress } = useProgressPersistence({ + store, + currentSongId, + isPlaying, + duration, + queueStartVersion, + currentTimeRef, + hasPlayer, + queueSeek, + resetPosition, + }) + + const resetQueuePlayback = useCallback(() => { + resetForNewQueue() + resetProgress() + setQueueStartVersion((version) => version + 1) + }, [resetForNewQueue, resetProgress]) const startPlayback = useCallback( ( recipe: ( state: playbackQueue.PlaybackQueueState ) => playbackQueue.PlaybackQueueState, - recorded: { - playlistId?: Id<"playlists"> - origin?: string - entity?: { kind: MusicEntityKind; id: string } - } = {} + recorded: RecordedPlay = {} ) => { playRequestRef.current += 1 queueOriginRef.current = @@ -870,47 +306,30 @@ export function PlaybackProvider({ children }: { children: ReactNode }) { if (!firstSongId) return setIsPlaying(true) - void (async () => { - const deadline = Date.now() + 20_000 - while ( - Date.now() < deadline && - store.getState().queue[0] === firstSongId - ) { - const player = playlistRef.current - if ( - player?.isLoaded && - player.playing && - loadedSongIdsRef.current[player.currentIndex] === firstSongId - ) { - await donateMusicPlay( - recorded.entity?.kind ?? - (recorded.playlistId ? "playlist" : "song"), - recorded.entity?.id ?? recorded.playlistId ?? firstSongId, - store.getState().shuffleEnabled - ) - return - } - await new Promise((resolve) => setTimeout(resolve, 250)) - } - })() + void waitForSong(firstSongId, true).then( + () => + donateMusicPlay( + recorded.entity?.kind ?? + (recorded.playlistId ? "playlist" : "song"), + recorded.entity?.id ?? recorded.playlistId ?? firstSongId, + store.getState().shuffleEnabled + ), + () => {} + ) recordPlay( recorded.playlistId ? { playlistId: recorded.playlistId } : { songId: firstSongId } ) }, - [store, resetQueuePlayback, setIsPlaying, recordPlay] + [store, resetQueuePlayback, setIsPlaying, waitForSong, recordPlay] ) const playCollection = useCallback( async ( songIds: Id<"songs">[] | Promise[]>, shuffled: boolean, - recorded: { - playlistId?: Id<"playlists"> - origin?: string - entity?: { kind: MusicEntityKind; id: string } - } = {} + recorded: RecordedPlay = {} ) => { const request = ++playRequestRef.current let resolvedSongIds: Id<"songs">[] @@ -932,21 +351,70 @@ export function PlaybackProvider({ children }: { children: ReactNode }) { [startPlayback] ) - const playInOrder = useCallback( - async (songIds: Id<"songs">[]) => { + const playSongs = useCallback( + (shuffled: boolean) => async (songIds: Id<"songs">[]) => { if (songIds.length === 0) return - startPlayback((state) => playbackQueue.startQueue(state, songIds, false)) + startPlayback((state) => + playbackQueue.startQueue(state, songIds, shuffled) + ) }, [startPlayback] ) + const playInOrder = useMemo(() => playSongs(false), [playSongs]) + const playShuffled = useMemo(() => playSongs(true), [playSongs]) - const playShuffled = useCallback( - async (songIds: Id<"songs">[]) => { - if (songIds.length === 0) return - startPlayback((state) => playbackQueue.startQueue(state, songIds, true)) - }, - [startPlayback] - ) + const collectionPlayers = useMemo(() => { + const collection = + (resolve: (...args: Args) => CollectionPlay) => + (shuffled: boolean) => + (...args: Args) => { + const { songIds, recorded } = resolve(...args) + return playCollection(songIds, shuffled, recorded) + } + const album = collection( + (albumId: Id<"albums">, songIds?: Id<"songs">[]) => ({ + songIds: + songIds ?? + fetchCachedQuery(convex, cacheScope, api.playlists.getAlbumSongIds, { + albumId, + }), + recorded: { + entity: { kind: "album", id: albumId }, + origin: `album:${albumId}`, + }, + }) + ) + const artist = collection((artistId: Id<"artists">) => ({ + songIds: fetchCachedQuery( + convex, + cacheScope, + api.playlists.getOrderedArtistSongIds, + { artistId } + ), + recorded: { entity: { kind: "artist", id: artistId } }, + })) + const playlist = collection( + (playlistId: Id<"playlists">, songIds?: Id<"songs">[]) => ({ + songIds: + songIds ?? + fetchCachedQuery( + convex, + cacheScope, + api.playlists.getPlaylistSongIds, + { playlistId } + ), + recorded: { playlistId }, + }) + ) + return { + playAlbum: album(false), + playAlbumShuffled: album(true), + playArtist: artist(false), + playArtistShuffled: artist(true), + playPlaylist: playlist(false), + playPlaylistShuffled: playlist(true), + } + }, [convex, cacheScope, playCollection]) const addToQueue = useCallback( (songIds: Id<"songs">[], position: "next" | "last") => { @@ -1022,85 +490,6 @@ export function PlaybackProvider({ children }: { children: ReactNode }) { [convex, cacheScope, store, startPlayback] ) - const loadAlbumSongIds = useCallback( - (albumId: Id<"albums">, songIds?: Id<"songs">[]) => - songIds ?? - fetchCachedQuery(convex, cacheScope, api.playlists.getAlbumSongIds, { - albumId, - }), - [convex, cacheScope] - ) - - const loadArtistSongIds = useCallback( - (artistId: Id<"artists">) => - fetchCachedQuery( - convex, - cacheScope, - api.playlists.getOrderedArtistSongIds, - { artistId } - ), - [convex, cacheScope] - ) - - const loadPlaylistSongIds = useCallback( - (playlistId: Id<"playlists">, songIds?: Id<"songs">[]) => - songIds ?? - fetchCachedQuery(convex, cacheScope, api.playlists.getPlaylistSongIds, { - playlistId, - }), - [convex, cacheScope] - ) - - const playAlbum = useCallback( - (albumId: Id<"albums">, songIds?: Id<"songs">[]) => - playCollection(loadAlbumSongIds(albumId, songIds), false, { - entity: { kind: "album", id: albumId }, - origin: `album:${albumId}`, - }), - [playCollection, loadAlbumSongIds] - ) - - const playAlbumShuffled = useCallback( - (albumId: Id<"albums">, songIds?: Id<"songs">[]) => - playCollection(loadAlbumSongIds(albumId, songIds), true, { - entity: { kind: "album", id: albumId }, - origin: `album:${albumId}`, - }), - [playCollection, loadAlbumSongIds] - ) - - const playArtist = useCallback( - (artistId: Id<"artists">) => - playCollection(loadArtistSongIds(artistId), false, { - entity: { kind: "artist", id: artistId }, - }), - [playCollection, loadArtistSongIds] - ) - - const playArtistShuffled = useCallback( - (artistId: Id<"artists">) => - playCollection(loadArtistSongIds(artistId), true, { - entity: { kind: "artist", id: artistId }, - }), - [playCollection, loadArtistSongIds] - ) - - const playPlaylist = useCallback( - (playlistId: Id<"playlists">, songIds?: Id<"songs">[]) => - playCollection(loadPlaylistSongIds(playlistId, songIds), false, { - playlistId, - }), - [playCollection, loadPlaylistSongIds] - ) - - const playPlaylistShuffled = useCallback( - (playlistId: Id<"playlists">, songIds?: Id<"songs">[]) => - playCollection(loadPlaylistSongIds(playlistId, songIds), true, { - playlistId, - }), - [playCollection, loadPlaylistSongIds] - ) - const toggleShuffle = useCallback(async () => { store.update(playbackQueue.toggleShuffle) }, [store]) @@ -1181,19 +570,19 @@ export function PlaybackProvider({ children }: { children: ReactNode }) { const skipToQueueIndex = useCallback( async (index: number) => { if (index <= 0) return - autoPlayNextRef.current = true + requestAutoPlay() store.update((state) => playbackQueue.skipToQueueIndex(state, index)) }, - [store] + [store, requestAutoPlay] ) const skipToHistoryIndex = useCallback( async (index: number) => { if (index < 0 || index >= store.getState().history.length) return - autoPlayNextRef.current = true + requestAutoPlay() store.update((state) => playbackQueue.skipToHistoryIndex(state, index)) }, - [store] + [store, requestAutoPlay] ) const skipNext = useCallback(async () => { @@ -1210,9 +599,9 @@ export function PlaybackProvider({ children }: { children: ReactNode }) { setIsPlaying(true) return } - autoPlayNextRef.current = true + requestAutoPlay() store.update(playbackQueue.skipNext) - }, [store, seekTo, setIsPlaying]) + }, [store, seekTo, setIsPlaying, requestAutoPlay]) const skipPrevious = useCallback(async () => { if (currentTimeRef.current > PREVIOUS_TRACK_RESTART_THRESHOLD_SECONDS) { @@ -1224,9 +613,9 @@ export function PlaybackProvider({ children }: { children: ReactNode }) { await seekTo(0) return } - autoPlayNextRef.current = true + requestAutoPlay() store.update(playbackQueue.skipPrevious) - }, [store, seekTo]) + }, [store, seekTo, currentTimeRef, requestAutoPlay]) // Queue changes return before React has loaded the native audio source. Siri // must observe the native player, rather than treating a state update as success. @@ -1234,32 +623,14 @@ export function PlaybackProvider({ children }: { children: ReactNode }) { async (playing?: boolean) => { const expectedId = store.getState().queue[0] if (!expectedId) throw new Error("Nothing is queued to play.") - const deadline = Date.now() + 20_000 - while (Date.now() < deadline) { - if (store.getState().queue[0] !== expectedId) { - throw new Error( - "The playback queue changed before the request finished." - ) - } - const player = playlistRef.current - if ( - player && - loadedSongIdsRef.current[player.currentIndex] === expectedId && - player.isLoaded && - (playing === undefined || player.playing === playing) - ) - return - await new Promise((resolve) => setTimeout(resolve, 100)) - } - throw new Error( - "The audio did not become ready. Check your connection and try again." - ) + await waitForSong(expectedId, playing) }, - [store] + [store, waitForSong] ) const actionsValue = useMemo( () => ({ + ...collectionPlayers, setIsPlaying, playInOrder, playShuffled, @@ -1267,12 +638,6 @@ export function PlaybackProvider({ children }: { children: ReactNode }) { addToQueueLast, playFromSong, playFromLibrarySong, - playAlbum, - playAlbumShuffled, - playArtist, - playArtistShuffled, - playPlaylist, - playPlaylistShuffled, toggleShuffle, setShuffleEnabled, cycleRepeat, @@ -1294,20 +659,15 @@ export function PlaybackProvider({ children }: { children: ReactNode }) { clearHistory, clearQueue, clearUpNext, + collectionPlayers, cycleRepeat, prepareQueue, startFromSong, setRepeatMode, setShuffleEnabled, - playAlbum, - playAlbumShuffled, - playArtist, - playArtistShuffled, playFromLibrarySong, playFromSong, playInOrder, - playPlaylist, - playPlaylistShuffled, playShuffled, reorderQueue, setIsPlaying, diff --git a/apps/mobile/hooks/use-native-playlist-sync.ts b/apps/mobile/hooks/use-native-playlist-sync.ts new file mode 100644 index 0000000..9b6660e --- /dev/null +++ b/apps/mobile/hooks/use-native-playlist-sync.ts @@ -0,0 +1,683 @@ +import { dolbyAtmosSupported } from "@/lib/playback-capabilities" +import { + createAudioPlaylist, + setAudioModeAsync, + setIsAudioActiveAsync, + type AudioMetadata, + type AudioPlaylist, + type AudioPlaylistStatus, + type AudioSource, +} from "expo-audio" +import { useConvex } from "convex/react" +import { useCallback, useEffect, useMemo, useRef, useState } from "react" + +import { useCacheScope } from "@/contexts/persistent-cache" +import { useServerConfig } from "@/contexts/server-config" +import { useCachedQuery } from "@/hooks/use-cached-query" +import { api } from "@otunes/convex/_generated/api" +import type { Id } from "@otunes/convex/_generated/dataModel" +import type { BrowseSong } from "@otunes/convex/types" +import { playbackStreamPlan } from "@otunes/convex/playback-stream" +import * as playbackQueue from "@otunes/convex/client/playback-queue" +import type { RepeatMode } from "@otunes/convex/client/playback-queue" +import type { PlaybackStore } from "@otunes/convex/client/playback-store" +import { getAuthCookieHeader } from "@/lib/auth-cookies" +import { loadPlaybackSongs } from "@/lib/playback-files" +import { showError } from "@/lib/toast" +import { albumCoverUrl, songAudioSource } from "@/lib/urls" +import { + activatePlaylistLockScreen, + releaseAudioPlaylist, + updatePlaylistLockScreenMetadata, +} from "@/lib/audio-playlist-lock-screen" +import { + nativePlaylistEdits, + nativePlaylistNeedsRebuild, + nativePlaylistSongIds, + repeatToPlaylistLoop, +} from "@/lib/native-playlist-window" + +const PLAYBACK_READY_TIMEOUT_MS = 20_000 + +function lockScreenMetadata( + song: BrowseSong, + serverUrl: string +): AudioMetadata { + return { + title: song.title, + artist: song.artist.name, + albumTitle: song.album.title, + artworkUrl: albumCoverUrl(serverUrl, song.album.id, 512), + } +} + +type PlaybackWaiter = { playing?: boolean; resolve: () => void } + +/** Where the last sync left the native player, and the queue it reflects. */ +type SyncedPosition = { + index: number + songId: Id<"songs"> | undefined + historyLength: number +} + +/** + * Keeps the native `AudioPlaylist` in step with the playback queue, and + * feeds its status (position, playing, track changes) back into the queue. + */ +export function useNativePlaylistSync({ + store, + queueSongIds, + queueHistorySongIds, + repeatEnabled, + currentSong, + queueStartVersion, +}: { + store: PlaybackStore + queueSongIds: Id<"songs">[] + queueHistorySongIds: Id<"songs">[] + repeatEnabled: RepeatMode + currentSong: BrowseSong | null + queueStartVersion: number +}) { + const convex = useConvex() + const { config } = useServerConfig() + const cacheScope = useCacheScope() + const user = useCachedQuery(api.auth.currentUser) + const storeRef = useRef(store) + storeRef.current = store + + const playlistRef = useRef(null) + const statusSubscriptionRef = useRef<{ remove: () => void } | null>(null) + const trackChangedSubscriptionRef = useRef<{ remove: () => void } | null>( + null + ) + const loadedSongIdsRef = useRef[]>([]) + const loadedLoadKeyRef = useRef(null) + const syncedPositionRef = useRef(null) + const waitersRef = useRef(new Map, Set>()) + const autoPlayNextRef = useRef(false) + const isPlayingRef = useRef(false) + const currentTimeRef = useRef(0) + const pendingSeekRef = useRef(null) + const seekInFlightRef = useRef(false) + const loadRequestIdRef = useRef(0) + + const [isPlaying, setIsPlayingState] = useState(false) + const [currentTime, setCurrentTime] = useState(0) + const [measuredDuration, setDuration] = useState(0) + + const currentSongId = queueSongIds[0] ?? null + const duration = + measuredDuration > 0 + ? measuredDuration + : (currentSong?.durationSeconds ?? 0) + const queueSyncKey = useMemo( + () => JSON.stringify(queueSongIds), + [queueSongIds] + ) + const historySyncKey = useMemo( + () => JSON.stringify(queueHistorySongIds), + [queueHistorySongIds] + ) + const currentSongSyncKey = currentSong + ? JSON.stringify([ + currentSong.id, + currentSong.title, + currentSong.artist.name, + currentSong.album.id, + currentSong.album.title, + currentSong.album.cover ?? "", + ]) + : "" + const userPlaybackKey = user + ? JSON.stringify([ + user._id, + user.playbackQuality, + dolbyAtmosSupported && (user.preferAtmos ?? false), + ]) + : "" + + const settleWaiters = useCallback(() => { + const player = playlistRef.current + if (waitersRef.current.size === 0 || !player) return + const songId = loadedSongIdsRef.current[player.currentIndex] + const waiters = songId && waitersRef.current.get(songId) + if (!waiters || !player.isLoaded) return + for (const waiter of waiters) { + if (waiter.playing === undefined || player.playing === waiter.playing) { + waiter.resolve() + } + } + }, []) + + const markSynced = useCallback( + (playlist: AudioPlaylist, songId: Id<"songs">, historyLength: number) => { + syncedPositionRef.current = { + index: playlist.currentIndex, + songId, + historyLength, + } + settleWaiters() + }, + [settleWaiters] + ) + + const destroyPlaylist = useCallback(() => { + statusSubscriptionRef.current?.remove() + statusSubscriptionRef.current = null + trackChangedSubscriptionRef.current?.remove() + trackChangedSubscriptionRef.current = null + const playlist = playlistRef.current + if (playlist) { + playlist.pause() + releaseAudioPlaylist(playlist) + } + playlistRef.current = null + loadedSongIdsRef.current = [] + loadedLoadKeyRef.current = null + syncedPositionRef.current = null + }, []) + + const resetPosition = useCallback(() => { + pendingSeekRef.current = null + currentTimeRef.current = 0 + setCurrentTime(0) + setDuration(0) + }, []) + + // A new queue must not inherit the native player's position or a pending seek. + const resetForNewQueue = useCallback(() => { + loadRequestIdRef.current += 1 + destroyPlaylist() + seekInFlightRef.current = false + resetPosition() + }, [destroyPlaylist, resetPosition]) + + const handlePlaylistStatusUpdate = useCallback( + (status: AudioPlaylistStatus) => { + settleWaiters() + if (!status.isLoaded) return + const state = storeRef.current.getState() + if (loadedSongIdsRef.current[status.currentIndex] !== state.queue[0]) { + return + } + + if (status.duration > 0) { + setDuration(status.duration) + } + + if (pendingSeekRef.current !== null && pendingSeekRef.current > 0) { + const seekSeconds = pendingSeekRef.current + pendingSeekRef.current = null + const playlist = playlistRef.current + if (playlist) { + // Statuses report the old position until the seek lands. + seekInFlightRef.current = true + void playlist.seekTo(seekSeconds).finally(() => { + seekInFlightRef.current = false + }) + } + currentTimeRef.current = seekSeconds + setCurrentTime(seekSeconds) + } else if (!seekInFlightRef.current) { + currentTimeRef.current = status.currentTime + setCurrentTime(status.currentTime) + } + + if (status.playing) { + autoPlayNextRef.current = false + if (!isPlayingRef.current) { + isPlayingRef.current = true + setIsPlayingState(true) + } + } else if (isPlayingRef.current && !status.didJustFinish) { + autoPlayNextRef.current = false + isPlayingRef.current = false + setIsPlayingState(false) + } + + if (!status.didJustFinish) return + if (state.repeatEnabled === "once") return + if (status.currentIndex < status.trackCount - 1) return + + const shouldContinue = + state.repeatEnabled !== "none" || state.queue.length > 1 + if (!shouldContinue) { + autoPlayNextRef.current = false + isPlayingRef.current = false + setIsPlayingState(false) + storeRef.current.update(playbackQueue.skipNext) + void playlistRef.current?.seekTo(0) + currentTimeRef.current = 0 + setCurrentTime(0) + return + } + + autoPlayNextRef.current = true + isPlayingRef.current = true + setIsPlayingState(true) + storeRef.current.update(playbackQueue.skipNext) + if (storeRef.current.getState().queue[0] === state.queue[0]) { + const playlist = playlistRef.current + void playlist?.seekTo(0) + playlist?.play() + } + }, + [settleWaiters] + ) + + const handleTrackChanged = useCallback(() => { + const playlist = playlistRef.current + const synced = syncedPositionRef.current + if (!playlist || !synced) return + // Queue edits run synchronously, so an event can describe an index from + // before them (Android's `previousIndex` also goes stale). The player's + // live index is the one that matters; if it is where the last sync left + // it, this change was ours. + const index = playlist.currentIndex + if (index === synced.index) return + const state = storeRef.current.getState() + // A queue change is still being applied and will move the player itself. + if ( + state.queue[0] !== synced.songId || + state.history.length !== synced.historyLength + ) { + return + } + // The window is history then queue, so the current track sits at + // `history.length`. + const steps = index - state.history.length + if (steps === 0) return + + autoPlayNextRef.current = true + isPlayingRef.current = true + setIsPlayingState(true) + + storeRef.current.update((state) => { + if (steps === 1) return playbackQueue.skipNext(state) + if (steps > 1) return playbackQueue.skipToQueueIndex(state, steps) + let next = state + for (let i = 0; i < -steps; i++) { + next = playbackQueue.skipPrevious(next) + } + return next + }) + const next = storeRef.current.getState() + syncedPositionRef.current = { + index, + songId: next.queue[0], + historyLength: next.history.length, + } + settleWaiters() + }, [settleWaiters]) + + const ensurePlaylist = useCallback( + (sources: AudioSource[]) => { + if (playlistRef.current) { + return playlistRef.current + } + + const playlist = createAudioPlaylist({ + sources, + updateInterval: 500, + loop: repeatToPlaylistLoop(storeRef.current.getState().repeatEnabled), + }) + // System volume controls loudness; keep the player gain at full scale. + playlist.volume = 1 + statusSubscriptionRef.current = playlist.addListener( + "playlistStatusUpdate", + handlePlaylistStatusUpdate + ) + trackChangedSubscriptionRef.current = playlist.addListener( + "trackChanged", + handleTrackChanged + ) + playlistRef.current = playlist + return playlist + }, + [handlePlaylistStatusUpdate, handleTrackChanged] + ) + + useEffect(() => { + let cancelled = false + void setAudioModeAsync({ + playsInSilentMode: true, + shouldPlayInBackground: true, + interruptionMode: "doNotMix", + }).catch(() => { + if (!cancelled) { + showError("Audio playback could not be initialized.") + } + }) + + return () => { + cancelled = true + destroyPlaylist() + } + }, [destroyPlaylist]) + + useEffect(() => { + const playlist = playlistRef.current + if (playlist) { + playlist.loop = repeatToPlaylistLoop(repeatEnabled) + } + }, [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 seekTo = useCallback( + async (seconds: number) => { + const playlist = playlistRef.current + if (!playlist) return + const maxDuration = playlist.duration || duration || 0 + const clamped = Math.max(0, Math.min(seconds, maxDuration)) + await playlist.seekTo(clamped) + currentTimeRef.current = clamped + setCurrentTime(clamped) + }, + [duration] + ) + + const handlePlaybackLoadFailure = useCallback(() => { + autoPlayNextRef.current = false + isPlayingRef.current = false + destroyPlaylist() + setIsPlayingState(false) + showError("Could not play this track. The queue has been cleared.") + storeRef.current.update(playbackQueue.clearQueue) + }, [destroyPlaylist]) + + useEffect(() => { + const loadRequestId = ++loadRequestIdRef.current + const isCurrentLoad = () => loadRequestIdRef.current === loadRequestId + + async function syncPlaylist() { + if (!config || !currentSongId || !user) { + if (!currentSongId) { + destroyPlaylist() + } + return + } + + const windowSongIds = nativePlaylistSongIds( + queueHistorySongIds, + queueSongIds + ) + const currentIndex = queueHistorySongIds.length + const cookieHeader = await getAuthCookieHeader( + config.serverUrl, + config.convexUrl + ) + if (!cookieHeader) { + showError("Your session has expired. Please sign in again.") + return + } + if (!isCurrentLoad()) return + + const loadKey = `${user.playbackQuality}:${dolbyAtmosSupported && (user.preferAtmos ?? false)}:${cookieHeader}` + const needsRebuild = + loadedLoadKeyRef.current !== loadKey || + nativePlaylistNeedsRebuild( + loadedSongIdsRef.current, + queueHistorySongIds, + queueSongIds + ) + + const metadata = currentSong + ? lockScreenMetadata(currentSong, config.serverUrl) + : null + const shouldPlay = isPlayingRef.current || autoPlayNextRef.current + + if (!needsRebuild) { + const playlist = playlistRef.current + if (!playlist) return + if (playlist.currentIndex !== currentIndex) { + playlist.skipTo(currentIndex) + } + playlist.loop = repeatToPlaylistLoop(repeatEnabled) + if (metadata) { + updatePlaylistLockScreenMetadata(playlist, metadata) + } + markSynced(playlist, currentSongId, currentIndex) + if (shouldPlay) { + isPlayingRef.current = true + setIsPlayingState(true) + playlist.play() + } + return + } + + const buildSources = async (songIds: Id<"songs">[]) => { + const songs = await loadPlaybackSongs(convex, cacheScope, songIds) + const sources = new Map, AudioSource>() + for (const song of songs) { + if (!song) return null + const plan = playbackStreamPlan( + song.files, + user.playbackQuality, + dolbyAtmosSupported && (user.preferAtmos ?? false) + ) + sources.set( + song.id, + songAudioSource( + config.serverUrl, + song.id, + plan.playbackQuality, + plan.preferAtmos, + plan.useHls, + { cookieHeader, songTitle: song.title } + ) + ) + } + return sources + } + + // Queue and history edits around the playing track are applied in place + // so playback isn't interrupted. + const loadedPlaylist = playlistRef.current + const edits = + loadedPlaylist && loadedLoadKeyRef.current === loadKey + ? nativePlaylistEdits( + loadedSongIdsRef.current, + loadedPlaylist.currentIndex, + windowSongIds, + currentIndex + ) + : null + if (loadedPlaylist && edits) { + const addedSongIds = edits.flatMap((edit) => + edit.type === "remove" ? [] : [edit.songId] + ) + const sources = await buildSources([...new Set(addedSongIds)]) + if (!isCurrentLoad()) return + if (!sources) { + handlePlaybackLoadFailure() + return + } + for (const edit of edits) { + if (edit.type === "remove") { + loadedPlaylist.remove(edit.index) + } else if (edit.type === "insert") { + loadedPlaylist.insert(sources.get(edit.songId)!, edit.index) + } else { + loadedPlaylist.add(sources.get(edit.songId)!) + } + } + loadedSongIdsRef.current = windowSongIds + loadedPlaylist.loop = repeatToPlaylistLoop(repeatEnabled) + markSynced(loadedPlaylist, currentSongId, currentIndex) + if (shouldPlay && !loadedPlaylist.playing) { + isPlayingRef.current = true + setIsPlayingState(true) + loadedPlaylist.play() + } + return + } + + const sourcesById = await buildSources(windowSongIds) + if (!isCurrentLoad()) return + if (!sourcesById) { + handlePlaybackLoadFailure() + return + } + const sources = windowSongIds.map((songId) => sourcesById.get(songId)!) + + // Keep the position when only the stream settings changed. + const resumeSeconds = + loadedPlaylist && + loadedSongIdsRef.current[loadedPlaylist.currentIndex] === currentSongId + ? currentTimeRef.current + : null + + destroyPlaylist() + + if (resumeSeconds !== null && resumeSeconds > 0) { + pendingSeekRef.current = resumeSeconds + } + // Start at the current track and put history in front of it, so an + // earlier song is never loaded first. + const playlist = ensurePlaylist(sources.slice(currentIndex)) + for (let index = 0; index < currentIndex; index++) { + playlist.insert(sources[index], index) + } + loadedSongIdsRef.current = windowSongIds + loadedLoadKeyRef.current = loadKey + playlist.loop = repeatToPlaylistLoop(repeatEnabled) + activatePlaylistLockScreen( + playlist, + metadata ?? { title: currentSong?.title } + ) + markSynced(playlist, currentSongId, currentIndex) + + if (shouldPlay) { + isPlayingRef.current = true + setIsPlayingState(true) + playlist.play() + } else { + playlist.pause() + // Enable audio for the restored queue without starting playback. + try { + await setIsAudioActiveAsync(true) + } catch { + if (isCurrentLoad()) { + showError("Audio playback could not be initialized.") + } + } + } + } + + void syncPlaylist().catch(() => { + if (!isCurrentLoad()) return + handlePlaybackLoadFailure() + }) + + return () => { + if (loadRequestIdRef.current === loadRequestId) { + loadRequestIdRef.current += 1 + } + } + }, [ + cacheScope, + config, + convex, + currentSongId, + currentSongSyncKey, + destroyPlaylist, + ensurePlaylist, + handlePlaybackLoadFailure, + historySyncKey, + markSynced, + queueSyncKey, + queueStartVersion, + repeatEnabled, + userPlaybackKey, + ]) + + /** + * Resolves once the native player has `songId` loaded (and playing or + * paused, if given). Rejects if the queue moves to another song first. + */ + const waitForSong = useCallback( + (songId: Id<"songs">, playing?: boolean) => + new Promise((resolve, reject) => { + const store = storeRef.current + const waiters = waitersRef.current + let songWaiters = waiters.get(songId) + if (!songWaiters) { + songWaiters = new Set() + waiters.set(songId, songWaiters) + } + const finish = (error?: Error) => { + clearTimeout(timeout) + unsubscribe() + songWaiters.delete(waiter) + if (songWaiters.size === 0 && waiters.get(songId) === songWaiters) { + waiters.delete(songId) + } + if (error) reject(error) + else resolve() + } + const waiter: PlaybackWaiter = { playing, resolve: () => finish() } + const timeout = setTimeout( + () => + finish( + new Error( + "The audio did not become ready. Check your connection and try again." + ) + ), + PLAYBACK_READY_TIMEOUT_MS + ) + // Starting a queue clears it and fills it in one go; only the + // settled queue counts. + const unsubscribe = store.subscribe(() => { + queueMicrotask(() => { + if (store.getState().queue[0] !== songId) { + finish( + new Error( + "The playback queue changed before the request finished." + ) + ) + } + }) + }) + songWaiters.add(waiter) + settleWaiters() + }), + [settleWaiters] + ) + + const queueSeek = useCallback((seconds: number) => { + pendingSeekRef.current = seconds + }, []) + + const hasPlayer = useCallback(() => playlistRef.current !== null, []) + + const requestAutoPlay = useCallback(() => { + autoPlayNextRef.current = true + }, []) + + return { + isPlaying, + setIsPlaying, + currentTime, + currentTimeRef, + duration, + seekTo, + queueSeek, + hasPlayer, + requestAutoPlay, + resetPosition, + resetForNewQueue, + waitForSong, + } +} diff --git a/apps/mobile/hooks/use-progress-persistence.ts b/apps/mobile/hooks/use-progress-persistence.ts new file mode 100644 index 0000000..906e1c6 --- /dev/null +++ b/apps/mobile/hooks/use-progress-persistence.ts @@ -0,0 +1,128 @@ +import { useMutation } from "convex/react" +import { useCallback, useEffect, useRef, useState, type RefObject } from "react" +import { AppState } from "react-native" + +import { api } from "@otunes/convex/_generated/api" +import type { Id } from "@otunes/convex/_generated/dataModel" +import type { PlaybackStore } from "@otunes/convex/client/playback-store" +import { flushPersistentCache } from "@/lib/persistent-cache" + +const PROGRESS_SAVE_INTERVAL_MS = 5000 + +/** + * Restores the saved position when a song becomes current, and saves the + * position while it plays, on pause, and when the app leaves the foreground. + */ +export function useProgressPersistence({ + store, + currentSongId, + isPlaying, + duration, + queueStartVersion, + currentTimeRef, + hasPlayer, + queueSeek, + resetPosition, +}: { + store: PlaybackStore + currentSongId: Id<"songs"> | null + isPlaying: boolean + duration: number + queueStartVersion: number + currentTimeRef: RefObject + hasPlayer: () => boolean + queueSeek: (seconds: number) => void + resetPosition: () => void +}) { + const updateSongProgressMutation = useMutation( + api.playback.updateSongProgress + ) + const storeRef = useRef(store) + storeRef.current = store + + const progressRestoredForSongRef = useRef | null>(null) + const lastSavedProgressRef = useRef(-1) + const prevIsPlayingRef = useRef(false) + const [progressToRestore, setProgressToRestore] = useState( + null + ) + + const saveProgress = useCallback( + (seconds: number) => { + const songId = storeRef.current.getState().queue[0] + if (!songId) return + const progressSeconds = Math.max(0, Math.floor(seconds)) + if (progressSeconds === lastSavedProgressRef.current) return + lastSavedProgressRef.current = progressSeconds + storeRef.current.setProgress(songId, progressSeconds) + void updateSongProgressMutation({ + songId, + progressSeconds, + }).catch(() => {}) + }, + [updateSongProgressMutation] + ) + + const resetProgress = useCallback(() => { + progressRestoredForSongRef.current = null + lastSavedProgressRef.current = -1 + setProgressToRestore(null) + }, []) + + useEffect(() => { + if (!currentSongId) { + progressRestoredForSongRef.current = null + return + } + + if (progressRestoredForSongRef.current === currentSongId) return + 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]) + + useEffect(() => { + if (!currentSongId) return + if (prevIsPlayingRef.current === isPlaying) return + prevIsPlayingRef.current = isPlaying + saveProgress(currentTimeRef.current) + }, [isPlaying, currentSongId, currentTimeRef, saveProgress]) + + useEffect(() => { + const subscription = AppState.addEventListener("change", (state) => { + if (state === "active") return + const restorePending = progressToRestore !== null && progressToRestore > 0 + if (currentSongId && hasPlayer() && !restorePending) { + saveProgress(currentTimeRef.current) + } + flushPersistentCache() + }) + return () => subscription.remove() + }, [ + currentSongId, + currentTimeRef, + hasPlayer, + progressToRestore, + saveProgress, + ]) + + useEffect(() => { + if (!isPlaying || !currentSongId) return + const interval = setInterval(() => { + saveProgress(currentTimeRef.current) + }, PROGRESS_SAVE_INTERVAL_MS) + return () => clearInterval(interval) + }, [isPlaying, currentSongId, currentTimeRef, saveProgress]) + + return { resetProgress } +} -- 2.54.1 From bb817d8af055015ce88d4af64b26627ba3fe1b69 Mon Sep 17 00:00:00 2001 From: Joshua Higgins Date: Mon, 16 Nov 2026 20:19:00 -0500 Subject: [PATCH 2/3] fix(mobile): show restored position before playback resumes A paused player sends no status updates after it loads, so a restored position waited until Play to apply and the slider showed 0:00. Seek right away when the song is already loaded, and show the position. Co-Authored-By: Claude Opus 5.5 --- apps/mobile/hooks/use-native-playlist-sync.ts | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/apps/mobile/hooks/use-native-playlist-sync.ts b/apps/mobile/hooks/use-native-playlist-sync.ts index 9b6660e..f2fa1bf 100644 --- a/apps/mobile/hooks/use-native-playlist-sync.ts +++ b/apps/mobile/hooks/use-native-playlist-sync.ts @@ -656,7 +656,28 @@ export function useNativePlaylistSync({ [settleWaiters] ) + /** + * 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. + */ const queueSeek = useCallback((seconds: number) => { + 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 }, []) -- 2.54.1 From 8695c7e7c14bac11abfa1599df1da00366a5a067 Mon Sep 17 00:00:00 2001 From: Joshua Higgins Date: Mon, 16 Nov 2026 20:35:19 -0500 Subject: [PATCH 3/3] 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 -- 2.54.1