|
|
|
@@ -1,9 +1,10 @@
|
|
|
|
|
import {
|
|
|
|
|
createAudioPlayer,
|
|
|
|
|
createAudioPlaylist,
|
|
|
|
|
setAudioModeAsync,
|
|
|
|
|
type AudioMetadata,
|
|
|
|
|
type AudioPlayer,
|
|
|
|
|
type AudioStatus,
|
|
|
|
|
type AudioPlaylist,
|
|
|
|
|
type AudioPlaylistStatus,
|
|
|
|
|
type AudioSource,
|
|
|
|
|
} from "expo-audio"
|
|
|
|
|
import { useConvex, useMutation, useQuery } from "convex/react"
|
|
|
|
|
import {
|
|
|
|
@@ -26,6 +27,16 @@ import { playbackStreamPlan } from "@otunes/convex/playback-stream"
|
|
|
|
|
import { getAuthCookieHeader } from "@/lib/auth-cookies"
|
|
|
|
|
import { showError } from "@/lib/toast"
|
|
|
|
|
import { albumCoverUrl, songAudioSource } from "@/lib/urls"
|
|
|
|
|
import {
|
|
|
|
|
activatePlaylistLockScreen,
|
|
|
|
|
releaseAudioPlaylist,
|
|
|
|
|
updatePlaylistLockScreenMetadata,
|
|
|
|
|
} from "@/lib/audio-playlist-lock-screen"
|
|
|
|
|
import {
|
|
|
|
|
nativePlaylistNeedsRebuild,
|
|
|
|
|
nativePlaylistSongIds,
|
|
|
|
|
repeatToPlaylistLoop,
|
|
|
|
|
} from "@/lib/native-playlist-window"
|
|
|
|
|
|
|
|
|
|
type RepeatMode = "none" | "once" | "always"
|
|
|
|
|
|
|
|
|
@@ -33,12 +44,6 @@ const PREVIOUS_TRACK_RESTART_THRESHOLD_SECONDS = 5
|
|
|
|
|
const VOLUME_STORAGE_KEY = "otunes:volume"
|
|
|
|
|
const DEFAULT_VOLUME = 0.8
|
|
|
|
|
|
|
|
|
|
function statusLooksPaused(status: AudioStatus) {
|
|
|
|
|
const timeControlStatus = status.timeControlStatus.toLowerCase()
|
|
|
|
|
const playbackState = status.playbackState.toLowerCase()
|
|
|
|
|
return timeControlStatus.includes("paused") || playbackState.includes("paused")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function lockScreenMetadata(
|
|
|
|
|
song: BrowseSong,
|
|
|
|
|
serverUrl: string
|
|
|
|
@@ -121,9 +126,15 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
|
|
|
|
|
const user = useQuery(api.auth.currentUser)
|
|
|
|
|
const playback = useQuery(api.playback.getPlaybackState)
|
|
|
|
|
|
|
|
|
|
const playerRef = useRef<AudioPlayer | null>(null)
|
|
|
|
|
const playlistRef = useRef<AudioPlaylist | null>(null)
|
|
|
|
|
const statusSubscriptionRef = useRef<{ remove: () => void } | null>(null)
|
|
|
|
|
const loadedSongKeyRef = useRef<string | null>(null)
|
|
|
|
|
const trackChangedSubscriptionRef = useRef<{ remove: () => void } | null>(
|
|
|
|
|
null
|
|
|
|
|
)
|
|
|
|
|
const loadedSongIdsRef = useRef<Id<"songs">[]>([])
|
|
|
|
|
const loadedLoadKeyRef = useRef<string | null>(null)
|
|
|
|
|
const ignoreTrackChangeRef = useRef(false)
|
|
|
|
|
const applyingFromNativeRef = useRef(false)
|
|
|
|
|
const autoPlayNextRef = useRef(false)
|
|
|
|
|
const isPlayingRef = useRef(false)
|
|
|
|
|
const progressRestoredForSongRef = useRef<Id<"songs"> | null>(null)
|
|
|
|
@@ -131,21 +142,18 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
|
|
|
|
|
const prevIsPlayingRef = useRef(false)
|
|
|
|
|
const currentTimeRef = useRef(0)
|
|
|
|
|
const pendingSeekRef = useRef<number | null>(null)
|
|
|
|
|
const prevSongIdRef = useRef<Id<"songs"> | null>(null)
|
|
|
|
|
const pendingSongChangeRef = useRef(false)
|
|
|
|
|
const currentSongIdRef = useRef<Id<"songs"> | null>(null)
|
|
|
|
|
const loadedSongIdRef = useRef<Id<"songs"> | null>(null)
|
|
|
|
|
const loadRequestIdRef = useRef(0)
|
|
|
|
|
const handlingEndRef = useRef(false)
|
|
|
|
|
// While set, block playback of this song so skip-while-paused cannot
|
|
|
|
|
// restart the current track before the queue advances.
|
|
|
|
|
const skippingFromSongIdRef = useRef<Id<"songs"> | null>(null)
|
|
|
|
|
const volumeRef = useRef(DEFAULT_VOLUME)
|
|
|
|
|
|
|
|
|
|
const [isPlaying, setIsPlayingState] = useState(false)
|
|
|
|
|
const [currentTime, setCurrentTime] = useState(0)
|
|
|
|
|
const [duration, setDuration] = useState(0)
|
|
|
|
|
const [volume, setVolumeState] = useState(DEFAULT_VOLUME)
|
|
|
|
|
const [progressToRestore, setProgressToRestore] = useState<number | null>(null)
|
|
|
|
|
const [progressToRestore, setProgressToRestore] = useState<number | null>(
|
|
|
|
|
null
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const playInOrderMutation = useMutation(api.playback.playInOrder)
|
|
|
|
|
const playShuffledMutation = useMutation(api.playback.playShuffled)
|
|
|
|
@@ -162,11 +170,15 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
|
|
|
|
|
const skipToQueueIndexMutation = useMutation(api.playback.skipToQueueIndex)
|
|
|
|
|
const skipNextMutation = useMutation(api.playback.skipNext)
|
|
|
|
|
const skipPreviousMutation = useMutation(api.playback.skipPrevious)
|
|
|
|
|
const updateSongProgressMutation = useMutation(api.playback.updateSongProgress)
|
|
|
|
|
const updateSongProgressMutation = useMutation(
|
|
|
|
|
api.playback.updateSongProgress
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const currentSong = playback?.currentSong ?? null
|
|
|
|
|
const repeatEnabled = playback?.repeatEnabled ?? "none"
|
|
|
|
|
const queueLength = playback?.queueSongIds.length ?? 0
|
|
|
|
|
const queueSongIds = playback?.queueSongIds ?? []
|
|
|
|
|
const queueHistorySongIds = playback?.queueHistorySongIds ?? []
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
currentSongIdRef.current = currentSong?.id ?? null
|
|
|
|
@@ -174,6 +186,7 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
|
|
|
|
|
|
|
|
|
|
const repeatEnabledRef = useRef(repeatEnabled)
|
|
|
|
|
const queueLengthRef = useRef(queueLength)
|
|
|
|
|
const historyLengthRef = useRef(queueHistorySongIds.length)
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
repeatEnabledRef.current = repeatEnabled
|
|
|
|
@@ -183,74 +196,82 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
|
|
|
|
|
queueLengthRef.current = queueLength
|
|
|
|
|
}, [queueLength])
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
historyLengthRef.current = queueHistorySongIds.length
|
|
|
|
|
}, [queueHistorySongIds.length])
|
|
|
|
|
|
|
|
|
|
const skipNextMutationRef = useRef(skipNextMutation)
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
skipNextMutationRef.current = skipNextMutation
|
|
|
|
|
}, [skipNextMutation])
|
|
|
|
|
|
|
|
|
|
const destroyPlayer = useCallback(() => {
|
|
|
|
|
const skipPreviousMutationRef = useRef(skipPreviousMutation)
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
skipPreviousMutationRef.current = skipPreviousMutation
|
|
|
|
|
}, [skipPreviousMutation])
|
|
|
|
|
|
|
|
|
|
const skipToQueueIndexMutationRef = useRef(skipToQueueIndexMutation)
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
skipToQueueIndexMutationRef.current = skipToQueueIndexMutation
|
|
|
|
|
}, [skipToQueueIndexMutation])
|
|
|
|
|
|
|
|
|
|
const destroyPlaylist = useCallback(() => {
|
|
|
|
|
statusSubscriptionRef.current?.remove()
|
|
|
|
|
statusSubscriptionRef.current = null
|
|
|
|
|
const player = playerRef.current
|
|
|
|
|
player?.pause()
|
|
|
|
|
player?.setActiveForLockScreen(false)
|
|
|
|
|
player?.remove()
|
|
|
|
|
playerRef.current = null
|
|
|
|
|
loadedSongKeyRef.current = null
|
|
|
|
|
loadedSongIdRef.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
|
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
|
|
const handlePlaybackStatusUpdate = useCallback((status: AudioStatus) => {
|
|
|
|
|
if (!status.isLoaded) return
|
|
|
|
|
if (loadedSongIdRef.current !== currentSongIdRef.current) return
|
|
|
|
|
if (
|
|
|
|
|
skippingFromSongIdRef.current !== null &&
|
|
|
|
|
loadedSongIdRef.current === skippingFromSongIdRef.current
|
|
|
|
|
) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
currentTimeRef.current = status.currentTime
|
|
|
|
|
setCurrentTime(status.currentTime)
|
|
|
|
|
if (status.duration > 0) {
|
|
|
|
|
setDuration(status.duration)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pendingSeekRef.current !== null && pendingSeekRef.current > 0) {
|
|
|
|
|
const seekSeconds = pendingSeekRef.current
|
|
|
|
|
pendingSeekRef.current = null
|
|
|
|
|
void playerRef.current?.seekTo(seekSeconds)
|
|
|
|
|
currentTimeRef.current = seekSeconds
|
|
|
|
|
setCurrentTime(seekSeconds)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (status.playing) {
|
|
|
|
|
autoPlayNextRef.current = false
|
|
|
|
|
if (!isPlayingRef.current) {
|
|
|
|
|
isPlayingRef.current = true
|
|
|
|
|
setIsPlayingState(true)
|
|
|
|
|
}
|
|
|
|
|
} else if (
|
|
|
|
|
isPlayingRef.current &&
|
|
|
|
|
!status.didJustFinish &&
|
|
|
|
|
statusLooksPaused(status)
|
|
|
|
|
) {
|
|
|
|
|
autoPlayNextRef.current = false
|
|
|
|
|
isPlayingRef.current = false
|
|
|
|
|
setIsPlayingState(false)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (status.didJustFinish) {
|
|
|
|
|
if (handlingEndRef.current) return
|
|
|
|
|
handlingEndRef.current = true
|
|
|
|
|
|
|
|
|
|
if (repeatEnabledRef.current === "once") {
|
|
|
|
|
void playerRef.current?.seekTo(0)
|
|
|
|
|
playerRef.current?.play()
|
|
|
|
|
handlingEndRef.current = false
|
|
|
|
|
const handlePlaylistStatusUpdate = useCallback(
|
|
|
|
|
(status: AudioPlaylistStatus) => {
|
|
|
|
|
if (!status.isLoaded) return
|
|
|
|
|
if (
|
|
|
|
|
loadedSongIdsRef.current[status.currentIndex] !==
|
|
|
|
|
currentSongIdRef.current
|
|
|
|
|
) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
currentTimeRef.current = status.currentTime
|
|
|
|
|
setCurrentTime(status.currentTime)
|
|
|
|
|
if (status.duration > 0) {
|
|
|
|
|
setDuration(status.duration)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pendingSeekRef.current !== null && pendingSeekRef.current > 0) {
|
|
|
|
|
const seekSeconds = pendingSeekRef.current
|
|
|
|
|
pendingSeekRef.current = null
|
|
|
|
|
void playlistRef.current?.seekTo(seekSeconds)
|
|
|
|
|
currentTimeRef.current = seekSeconds
|
|
|
|
|
setCurrentTime(seekSeconds)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 (handlingEndRef.current) return
|
|
|
|
|
if (repeatEnabledRef.current === "once") return
|
|
|
|
|
if (status.currentIndex < status.trackCount - 1) return
|
|
|
|
|
|
|
|
|
|
handlingEndRef.current = true
|
|
|
|
|
const shouldContinue =
|
|
|
|
|
repeatEnabledRef.current !== "none" || queueLengthRef.current > 1
|
|
|
|
|
if (shouldContinue) {
|
|
|
|
@@ -271,7 +292,7 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
|
|
|
|
|
|
|
|
|
|
autoPlayNextRef.current = false
|
|
|
|
|
isPlayingRef.current = false
|
|
|
|
|
void playerRef.current?.seekTo(0)
|
|
|
|
|
void playlistRef.current?.seekTo(0)
|
|
|
|
|
currentTimeRef.current = 0
|
|
|
|
|
setCurrentTime(0)
|
|
|
|
|
setIsPlayingState(false)
|
|
|
|
@@ -284,23 +305,72 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
|
|
|
|
|
.finally(() => {
|
|
|
|
|
handlingEndRef.current = false
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}, [])
|
|
|
|
|
},
|
|
|
|
|
[]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const ensurePlayer = useCallback(() => {
|
|
|
|
|
if (playerRef.current) {
|
|
|
|
|
return playerRef.current
|
|
|
|
|
}
|
|
|
|
|
const handleTrackChanged = useCallback(
|
|
|
|
|
(data: { currentIndex: number; previousIndex: number }) => {
|
|
|
|
|
if (ignoreTrackChangeRef.current) return
|
|
|
|
|
if (data.currentIndex === data.previousIndex) return
|
|
|
|
|
|
|
|
|
|
const player = createAudioPlayer(null, { updateInterval: 500 })
|
|
|
|
|
player.volume = volume
|
|
|
|
|
statusSubscriptionRef.current = player.addListener(
|
|
|
|
|
"playbackStatusUpdate",
|
|
|
|
|
handlePlaybackStatusUpdate
|
|
|
|
|
)
|
|
|
|
|
playerRef.current = player
|
|
|
|
|
return player
|
|
|
|
|
}, [handlePlaybackStatusUpdate, volume])
|
|
|
|
|
applyingFromNativeRef.current = true
|
|
|
|
|
autoPlayNextRef.current = true
|
|
|
|
|
isPlayingRef.current = true
|
|
|
|
|
setIsPlayingState(true)
|
|
|
|
|
|
|
|
|
|
const steps = data.currentIndex - data.previousIndex
|
|
|
|
|
if (steps > 0) {
|
|
|
|
|
void (
|
|
|
|
|
steps === 1
|
|
|
|
|
? skipNextMutationRef.current()
|
|
|
|
|
: skipToQueueIndexMutationRef.current({ index: steps })
|
|
|
|
|
).catch(() => {
|
|
|
|
|
applyingFromNativeRef.current = false
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void (async () => {
|
|
|
|
|
for (let i = 0; i < -steps; i++) {
|
|
|
|
|
await skipPreviousMutationRef.current()
|
|
|
|
|
}
|
|
|
|
|
})().catch(() => {
|
|
|
|
|
applyingFromNativeRef.current = false
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
[]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const ensurePlaylist = useCallback(
|
|
|
|
|
(sources: AudioSource[]) => {
|
|
|
|
|
if (playlistRef.current) {
|
|
|
|
|
return playlistRef.current
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ignoreTrackChangeRef.current = true
|
|
|
|
|
const playlist = createAudioPlaylist({
|
|
|
|
|
sources,
|
|
|
|
|
updateInterval: 500,
|
|
|
|
|
loop: repeatToPlaylistLoop(repeatEnabledRef.current),
|
|
|
|
|
})
|
|
|
|
|
playlist.volume = volumeRef.current
|
|
|
|
|
statusSubscriptionRef.current = playlist.addListener(
|
|
|
|
|
"playlistStatusUpdate",
|
|
|
|
|
handlePlaylistStatusUpdate
|
|
|
|
|
)
|
|
|
|
|
trackChangedSubscriptionRef.current = playlist.addListener(
|
|
|
|
|
"trackChanged",
|
|
|
|
|
handleTrackChanged
|
|
|
|
|
)
|
|
|
|
|
playlistRef.current = playlist
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
ignoreTrackChangeRef.current = false
|
|
|
|
|
}, 150)
|
|
|
|
|
return playlist
|
|
|
|
|
},
|
|
|
|
|
[handlePlaylistStatusUpdate, handleTrackChanged]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
void setAudioModeAsync({
|
|
|
|
@@ -319,61 +389,52 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
destroyPlayer()
|
|
|
|
|
destroyPlaylist()
|
|
|
|
|
}
|
|
|
|
|
}, [destroyPlayer])
|
|
|
|
|
}, [destroyPlaylist])
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (playerRef.current) {
|
|
|
|
|
playerRef.current.volume = volume
|
|
|
|
|
volumeRef.current = volume
|
|
|
|
|
if (playlistRef.current) {
|
|
|
|
|
playlistRef.current.volume = volume
|
|
|
|
|
}
|
|
|
|
|
}, [volume])
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const playlist = playlistRef.current
|
|
|
|
|
if (playlist) {
|
|
|
|
|
playlist.loop = repeatToPlaylistLoop(repeatEnabled)
|
|
|
|
|
}
|
|
|
|
|
}, [repeatEnabled])
|
|
|
|
|
|
|
|
|
|
const setIsPlaying = useCallback((playing: boolean) => {
|
|
|
|
|
skippingFromSongIdRef.current = null
|
|
|
|
|
isPlayingRef.current = playing
|
|
|
|
|
setIsPlayingState(playing)
|
|
|
|
|
const player = playerRef.current
|
|
|
|
|
if (!player) return
|
|
|
|
|
const playlist = playlistRef.current
|
|
|
|
|
if (!playlist) return
|
|
|
|
|
if (playing) {
|
|
|
|
|
player.play()
|
|
|
|
|
playlist.play()
|
|
|
|
|
} else {
|
|
|
|
|
player.pause()
|
|
|
|
|
playlist.pause()
|
|
|
|
|
}
|
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
|
|
const beginQueueSkip = useCallback(() => {
|
|
|
|
|
skippingFromSongIdRef.current = currentSongIdRef.current
|
|
|
|
|
// Cancel in-flight loads so they cannot replace/restart the current track.
|
|
|
|
|
loadRequestIdRef.current += 1
|
|
|
|
|
|
|
|
|
|
// Never unpause the current track during a skip. Playback resumes only
|
|
|
|
|
// after the next song has been loaded.
|
|
|
|
|
playerRef.current?.pause()
|
|
|
|
|
isPlayingRef.current = false
|
|
|
|
|
setIsPlayingState(false)
|
|
|
|
|
autoPlayNextRef.current = true
|
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
|
|
const clearQueueSkip = useCallback(() => {
|
|
|
|
|
skippingFromSongIdRef.current = null
|
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
|
|
const setVolume = useCallback((nextVolume: number) => {
|
|
|
|
|
const clamped = Math.min(1, Math.max(0, nextVolume))
|
|
|
|
|
setVolumeState(clamped)
|
|
|
|
|
void AsyncStorage.setItem(VOLUME_STORAGE_KEY, String(clamped))
|
|
|
|
|
if (playerRef.current) {
|
|
|
|
|
playerRef.current.volume = clamped
|
|
|
|
|
if (playlistRef.current) {
|
|
|
|
|
playlistRef.current.volume = clamped
|
|
|
|
|
}
|
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
|
|
const seekTo = useCallback(
|
|
|
|
|
async (seconds: number) => {
|
|
|
|
|
const player = playerRef.current
|
|
|
|
|
if (!player) return
|
|
|
|
|
const clamped = Math.max(0, Math.min(seconds, duration || 0))
|
|
|
|
|
await player.seekTo(clamped)
|
|
|
|
|
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)
|
|
|
|
|
},
|
|
|
|
@@ -392,52 +453,31 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!currentSong) {
|
|
|
|
|
prevSongIdRef.current = null
|
|
|
|
|
loadedSongKeyRef.current = null
|
|
|
|
|
loadedSongIdRef.current = null
|
|
|
|
|
skippingFromSongIdRef.current = null
|
|
|
|
|
destroyPlayer()
|
|
|
|
|
progressRestoredForSongRef.current = null
|
|
|
|
|
applyingFromNativeRef.current = false
|
|
|
|
|
destroyPlaylist()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (prevSongIdRef.current === currentSong.id) return
|
|
|
|
|
|
|
|
|
|
const isInitialLoad = prevSongIdRef.current === null
|
|
|
|
|
prevSongIdRef.current = currentSong.id
|
|
|
|
|
loadedSongKeyRef.current = null
|
|
|
|
|
loadedSongIdRef.current = null
|
|
|
|
|
pendingSeekRef.current = null
|
|
|
|
|
progressRestoredForSongRef.current = null
|
|
|
|
|
currentTimeRef.current = 0
|
|
|
|
|
setCurrentTime(0)
|
|
|
|
|
setDuration(0)
|
|
|
|
|
setProgressToRestore(null)
|
|
|
|
|
lastSavedProgressRef.current = -1
|
|
|
|
|
if (!isInitialLoad) {
|
|
|
|
|
pendingSongChangeRef.current = true
|
|
|
|
|
destroyPlayer()
|
|
|
|
|
}
|
|
|
|
|
}, [currentSong?.id, destroyPlayer])
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!currentSong || !playback) return
|
|
|
|
|
if (progressRestoredForSongRef.current === currentSong.id) return
|
|
|
|
|
progressRestoredForSongRef.current = currentSong.id
|
|
|
|
|
lastSavedProgressRef.current = -1
|
|
|
|
|
if (pendingSongChangeRef.current) {
|
|
|
|
|
pendingSongChangeRef.current = false
|
|
|
|
|
currentTimeRef.current = 0
|
|
|
|
|
setCurrentTime(0)
|
|
|
|
|
setDuration(currentSong.durationSeconds)
|
|
|
|
|
if (applyingFromNativeRef.current) {
|
|
|
|
|
applyingFromNativeRef.current = false
|
|
|
|
|
setProgressToRestore(0)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
setProgressToRestore(playback.currentSongProgress ?? 0)
|
|
|
|
|
}, [currentSong?.id, playback])
|
|
|
|
|
setProgressToRestore(playback?.currentSongProgress ?? 0)
|
|
|
|
|
}, [currentSong?.id, currentSong, destroyPlaylist, playback])
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (progressToRestore === null || progressToRestore <= 0) return
|
|
|
|
|
if (!currentSong || !playerRef.current) return
|
|
|
|
|
if (!currentSong || !playlistRef.current) return
|
|
|
|
|
|
|
|
|
|
const maxDuration =
|
|
|
|
|
duration > 0 ? duration : progressToRestore
|
|
|
|
|
const maxDuration = duration > 0 ? duration : progressToRestore
|
|
|
|
|
const clamped = Math.min(progressToRestore, maxDuration)
|
|
|
|
|
pendingSeekRef.current = clamped
|
|
|
|
|
setProgressToRestore(null)
|
|
|
|
@@ -452,33 +492,21 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const loadRequestId = ++loadRequestIdRef.current
|
|
|
|
|
const isCurrentLoad = () =>
|
|
|
|
|
loadRequestIdRef.current === loadRequestId &&
|
|
|
|
|
currentSongIdRef.current === currentSong?.id
|
|
|
|
|
const isCurrentLoad = () => loadRequestIdRef.current === loadRequestId
|
|
|
|
|
|
|
|
|
|
async function loadSong() {
|
|
|
|
|
async function syncPlaylist() {
|
|
|
|
|
if (!config || !currentSong || user === undefined || !user) {
|
|
|
|
|
if (!currentSong) {
|
|
|
|
|
destroyPlayer()
|
|
|
|
|
destroyPlaylist()
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const song = await convex.query(api.library.getSong, {
|
|
|
|
|
id: currentSong.id,
|
|
|
|
|
})
|
|
|
|
|
if (!song || !isCurrentLoad()) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const plan = playbackStreamPlan(
|
|
|
|
|
song.files,
|
|
|
|
|
user.playbackQuality,
|
|
|
|
|
user.preferAtmos ?? false
|
|
|
|
|
const windowSongIds = nativePlaylistSongIds(
|
|
|
|
|
queueHistorySongIds,
|
|
|
|
|
queueSongIds
|
|
|
|
|
)
|
|
|
|
|
const loadKey = `${currentSong.id}:${plan.playbackQuality}:${plan.useHls ? "hls" : "file"}`
|
|
|
|
|
if (loadedSongKeyRef.current === loadKey) return
|
|
|
|
|
|
|
|
|
|
const currentIndex = queueHistorySongIds.length
|
|
|
|
|
const cookieHeader = await getAuthCookieHeader(
|
|
|
|
|
config.serverUrl,
|
|
|
|
|
config.convexUrl
|
|
|
|
@@ -487,87 +515,119 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
|
|
|
|
|
showError("Your session has expired. Please sign in again.")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if (!isCurrentLoad()) return
|
|
|
|
|
|
|
|
|
|
const loadKey = `${user.playbackQuality}:${user.preferAtmos ?? false}:${cookieHeader}`
|
|
|
|
|
const needsRebuild =
|
|
|
|
|
loadedLoadKeyRef.current !== loadKey ||
|
|
|
|
|
nativePlaylistNeedsRebuild(
|
|
|
|
|
loadedSongIdsRef.current,
|
|
|
|
|
queueHistorySongIds,
|
|
|
|
|
queueSongIds
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const metadata = lockScreenMetadata(currentSong, config.serverUrl)
|
|
|
|
|
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)
|
|
|
|
|
playlist.volume = volumeRef.current
|
|
|
|
|
updatePlaylistLockScreenMetadata(playlist, metadata)
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
ignoreTrackChangeRef.current = false
|
|
|
|
|
}, 150)
|
|
|
|
|
if (shouldPlay) {
|
|
|
|
|
isPlayingRef.current = true
|
|
|
|
|
setIsPlayingState(true)
|
|
|
|
|
playlist.play()
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const songs = await convex.query(api.library.getSongsForPlayback, {
|
|
|
|
|
ids: windowSongIds,
|
|
|
|
|
})
|
|
|
|
|
if (!isCurrentLoad()) return
|
|
|
|
|
|
|
|
|
|
const sources: AudioSource[] = []
|
|
|
|
|
for (let i = 0; i < windowSongIds.length; i++) {
|
|
|
|
|
const song = songs[i]
|
|
|
|
|
if (!song) {
|
|
|
|
|
showError("Could not play this track. The queue has been cleared.")
|
|
|
|
|
void clearQueueMutation()
|
|
|
|
|
setIsPlayingState(false)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
const plan = playbackStreamPlan(
|
|
|
|
|
song.files,
|
|
|
|
|
user.playbackQuality,
|
|
|
|
|
user.preferAtmos ?? false
|
|
|
|
|
)
|
|
|
|
|
sources.push(
|
|
|
|
|
songAudioSource(
|
|
|
|
|
config.serverUrl,
|
|
|
|
|
song.id,
|
|
|
|
|
plan.playbackQuality,
|
|
|
|
|
plan.preferAtmos,
|
|
|
|
|
plan.useHls,
|
|
|
|
|
{ cookieHeader, songTitle: song.title }
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ignoreTrackChangeRef.current = true
|
|
|
|
|
destroyPlaylist()
|
|
|
|
|
if (!isCurrentLoad()) {
|
|
|
|
|
ignoreTrackChangeRef.current = false
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const source = songAudioSource(
|
|
|
|
|
config.serverUrl,
|
|
|
|
|
currentSong.id,
|
|
|
|
|
plan.playbackQuality,
|
|
|
|
|
plan.preferAtmos,
|
|
|
|
|
plan.useHls,
|
|
|
|
|
{ cookieHeader, songTitle: currentSong.title }
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
skippingFromSongIdRef.current !== null &&
|
|
|
|
|
skippingFromSongIdRef.current === currentSong.id
|
|
|
|
|
) {
|
|
|
|
|
return
|
|
|
|
|
const playlist = ensurePlaylist(sources)
|
|
|
|
|
loadedSongIdsRef.current = windowSongIds
|
|
|
|
|
loadedLoadKeyRef.current = loadKey
|
|
|
|
|
playlist.volume = volumeRef.current
|
|
|
|
|
playlist.loop = repeatToPlaylistLoop(repeatEnabled)
|
|
|
|
|
if (currentIndex > 0) {
|
|
|
|
|
playlist.skipTo(currentIndex)
|
|
|
|
|
}
|
|
|
|
|
activatePlaylistLockScreen(playlist, metadata)
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
ignoreTrackChangeRef.current = false
|
|
|
|
|
}, 150)
|
|
|
|
|
|
|
|
|
|
const shouldPlayAfterLoad = isPlayingRef.current || autoPlayNextRef.current
|
|
|
|
|
|
|
|
|
|
destroyPlayer()
|
|
|
|
|
if (!isCurrentLoad()) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
skippingFromSongIdRef.current !== null &&
|
|
|
|
|
skippingFromSongIdRef.current === currentSong.id
|
|
|
|
|
) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const player = ensurePlayer()
|
|
|
|
|
loadedSongKeyRef.current = loadKey
|
|
|
|
|
loadedSongIdRef.current = currentSong.id
|
|
|
|
|
skippingFromSongIdRef.current = null
|
|
|
|
|
|
|
|
|
|
player.replace(source)
|
|
|
|
|
player.volume = volume
|
|
|
|
|
player.setActiveForLockScreen(
|
|
|
|
|
true,
|
|
|
|
|
lockScreenMetadata(currentSong, config.serverUrl),
|
|
|
|
|
{ showSeekForward: true, showSeekBackward: true }
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if (!isCurrentLoad()) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (shouldPlayAfterLoad) {
|
|
|
|
|
if (shouldPlay) {
|
|
|
|
|
isPlayingRef.current = true
|
|
|
|
|
setIsPlayingState(true)
|
|
|
|
|
player.play()
|
|
|
|
|
playlist.play()
|
|
|
|
|
} else {
|
|
|
|
|
player.pause()
|
|
|
|
|
playlist.pause()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setDuration(currentSong.durationSeconds)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void loadSong().catch(() => {
|
|
|
|
|
void syncPlaylist().catch(() => {
|
|
|
|
|
showError("Could not play this track. The queue has been cleared.")
|
|
|
|
|
void clearQueueMutation()
|
|
|
|
|
setIsPlayingState(false)
|
|
|
|
|
})
|
|
|
|
|
}, [
|
|
|
|
|
config,
|
|
|
|
|
currentSong?.id,
|
|
|
|
|
user?.playbackQuality,
|
|
|
|
|
user?.preferAtmos,
|
|
|
|
|
user === undefined,
|
|
|
|
|
volume,
|
|
|
|
|
destroyPlayer,
|
|
|
|
|
ensurePlayer,
|
|
|
|
|
clearQueueMutation,
|
|
|
|
|
currentSong,
|
|
|
|
|
user,
|
|
|
|
|
config,
|
|
|
|
|
convex,
|
|
|
|
|
currentSong,
|
|
|
|
|
destroyPlaylist,
|
|
|
|
|
ensurePlaylist,
|
|
|
|
|
queueHistorySongIds,
|
|
|
|
|
queueSongIds,
|
|
|
|
|
repeatEnabled,
|
|
|
|
|
user,
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
@@ -711,11 +771,11 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
|
|
|
|
|
|
|
|
|
|
const value = useMemo<PlaybackContextValue>(
|
|
|
|
|
() => ({
|
|
|
|
|
queueSongIds: playback?.queueSongIds ?? [],
|
|
|
|
|
queueHistorySongIds: playback?.queueHistorySongIds ?? [],
|
|
|
|
|
currentSong: playback?.currentSong ?? null,
|
|
|
|
|
queueSongIds,
|
|
|
|
|
queueHistorySongIds,
|
|
|
|
|
currentSong,
|
|
|
|
|
shuffleEnabled: playback?.shuffleEnabled ?? false,
|
|
|
|
|
repeatEnabled: playback?.repeatEnabled ?? "none",
|
|
|
|
|
repeatEnabled,
|
|
|
|
|
isPlaying: Boolean(currentSong && isPlaying),
|
|
|
|
|
volume,
|
|
|
|
|
currentTime,
|
|
|
|
@@ -750,28 +810,25 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
|
|
|
|
|
},
|
|
|
|
|
skipToQueueIndex: async (index) => {
|
|
|
|
|
if (index <= 0) return
|
|
|
|
|
beginQueueSkip()
|
|
|
|
|
autoPlayNextRef.current = true
|
|
|
|
|
await skipToQueueIndexMutation({ index })
|
|
|
|
|
},
|
|
|
|
|
skipNext: async () => {
|
|
|
|
|
const isFinishingQueue = repeatEnabled === "none" && queueLength <= 1
|
|
|
|
|
if (isFinishingQueue) {
|
|
|
|
|
clearQueueSkip()
|
|
|
|
|
setIsPlaying(false)
|
|
|
|
|
await seekTo(0)
|
|
|
|
|
await skipNextMutation()
|
|
|
|
|
await seekTo(0)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// These paths keep the same current song id — restart in place.
|
|
|
|
|
if (repeatEnabled === "once" || queueLength <= 1) {
|
|
|
|
|
clearQueueSkip()
|
|
|
|
|
await seekTo(0)
|
|
|
|
|
await skipNextMutation()
|
|
|
|
|
setIsPlaying(true)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
beginQueueSkip()
|
|
|
|
|
autoPlayNextRef.current = true
|
|
|
|
|
await skipNextMutation()
|
|
|
|
|
},
|
|
|
|
|
skipPrevious: async () => {
|
|
|
|
@@ -779,56 +836,56 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
|
|
|
|
|
await seekTo(0)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
const historyLength = playback?.queueHistorySongIds.length ?? 0
|
|
|
|
|
if (historyLength === 0) {
|
|
|
|
|
clearQueueSkip()
|
|
|
|
|
if (queueHistorySongIds.length === 0) {
|
|
|
|
|
await seekTo(0)
|
|
|
|
|
await skipPreviousMutation()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
beginQueueSkip()
|
|
|
|
|
autoPlayNextRef.current = true
|
|
|
|
|
await skipPreviousMutation()
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
[
|
|
|
|
|
playback,
|
|
|
|
|
currentSong,
|
|
|
|
|
isPlaying,
|
|
|
|
|
volume,
|
|
|
|
|
currentTime,
|
|
|
|
|
duration,
|
|
|
|
|
seekTo,
|
|
|
|
|
setIsPlaying,
|
|
|
|
|
setVolume,
|
|
|
|
|
playInOrder,
|
|
|
|
|
playShuffled,
|
|
|
|
|
addToQueueNext,
|
|
|
|
|
addToQueueLast,
|
|
|
|
|
playFromSong,
|
|
|
|
|
playFromLibrarySong,
|
|
|
|
|
addToQueueNext,
|
|
|
|
|
currentSong,
|
|
|
|
|
currentTime,
|
|
|
|
|
cycleRepeatMutation,
|
|
|
|
|
duration,
|
|
|
|
|
isPlaying,
|
|
|
|
|
playAlbum,
|
|
|
|
|
playAlbumShuffled,
|
|
|
|
|
playArtist,
|
|
|
|
|
playArtistShuffled,
|
|
|
|
|
playFromLibrarySong,
|
|
|
|
|
playFromSong,
|
|
|
|
|
playInOrder,
|
|
|
|
|
playPlaylist,
|
|
|
|
|
playPlaylistShuffled,
|
|
|
|
|
toggleShuffleMutation,
|
|
|
|
|
cycleRepeatMutation,
|
|
|
|
|
clearQueueMutation,
|
|
|
|
|
playShuffled,
|
|
|
|
|
playback?.shuffleEnabled,
|
|
|
|
|
queueHistorySongIds,
|
|
|
|
|
queueLength,
|
|
|
|
|
queueSongIds,
|
|
|
|
|
reorderQueueMutation,
|
|
|
|
|
skipToQueueIndexMutation,
|
|
|
|
|
repeatEnabled,
|
|
|
|
|
seekTo,
|
|
|
|
|
setIsPlaying,
|
|
|
|
|
setVolume,
|
|
|
|
|
skipNextMutation,
|
|
|
|
|
skipPreviousMutation,
|
|
|
|
|
beginQueueSkip,
|
|
|
|
|
clearQueueSkip,
|
|
|
|
|
repeatEnabled,
|
|
|
|
|
queueLength,
|
|
|
|
|
skipToQueueIndexMutation,
|
|
|
|
|
toggleShuffleMutation,
|
|
|
|
|
clearQueueMutation,
|
|
|
|
|
volume,
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<PlaybackActionsContext.Provider value={actionsValue}>
|
|
|
|
|
<PlaybackContext.Provider value={value}>{children}</PlaybackContext.Provider>
|
|
|
|
|
<PlaybackContext.Provider value={value}>
|
|
|
|
|
{children}
|
|
|
|
|
</PlaybackContext.Provider>
|
|
|
|
|
</PlaybackActionsContext.Provider>
|
|
|
|
|
)
|
|
|
|
|
}
|