fix(mobile): skipping tracks played half-second of old song (#12)

---------

Co-authored-by: Rem Helper <remhelper@abunchofknowitalls.com>
Reviewed-on: https://git.abunchofknowitalls.com/joshuafhiggins/otunes/pulls/12
This commit was merged in pull request #12.
This commit is contained in:
joshuafhigginsandremhelper committed 2026-07-15 02:42:57 +00:00
1 parent 8fbff071ea
commit 4fc04979aa
1 file changed
+78 -3
+78 -3
View File
@@ -137,6 +137,9 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
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 [isPlaying, setIsPlayingState] = useState(false)
const [currentTime, setCurrentTime] = useState(0)
@@ -200,6 +203,12 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
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)
@@ -321,6 +330,7 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
}, [volume])
const setIsPlaying = useCallback((playing: boolean) => {
skippingFromSongIdRef.current = null
isPlayingRef.current = playing
setIsPlayingState(playing)
const player = playerRef.current
@@ -332,6 +342,23 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
}
}, [])
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)
@@ -368,6 +395,7 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
prevSongIdRef.current = null
loadedSongKeyRef.current = null
loadedSongIdRef.current = null
skippingFromSongIdRef.current = null
destroyPlayer()
return
}
@@ -473,6 +501,13 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
{ cookieHeader, songTitle: currentSong.title }
)
if (
skippingFromSongIdRef.current !== null &&
skippingFromSongIdRef.current === currentSong.id
) {
return
}
const shouldPlayAfterLoad = isPlayingRef.current || autoPlayNextRef.current
destroyPlayer()
@@ -480,9 +515,17 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
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
@@ -492,8 +535,16 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
{ showSeekForward: true, showSeekBackward: true }
)
if (!isCurrentLoad()) {
return
}
if (shouldPlayAfterLoad) {
isPlayingRef.current = true
setIsPlayingState(true)
player.play()
} else {
player.pause()
}
setDuration(currentSong.durationSeconds)
@@ -698,24 +749,44 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
await reorderQueueMutation({ songIds })
},
skipToQueueIndex: async (index) => {
if (index <= 0) return
beginQueueSkip()
await skipToQueueIndexMutation({ index })
},
skipNext: async () => {
const isFinishingQueue = repeatEnabled === "none" && queueLength <= 1
if (isFinishingQueue) {
clearQueueSkip()
setIsPlaying(false)
await seekTo(0)
}
await skipNextMutation()
if (isFinishingQueue) {
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()
await skipNextMutation()
},
skipPrevious: async () => {
if (currentTime > PREVIOUS_TRACK_RESTART_THRESHOLD_SECONDS) {
await seekTo(0)
return
}
const historyLength = playback?.queueHistorySongIds.length ?? 0
if (historyLength === 0) {
clearQueueSkip()
await seekTo(0)
await skipPreviousMutation()
return
}
beginQueueSkip()
await skipPreviousMutation()
},
}),
@@ -748,6 +819,10 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
skipToQueueIndexMutation,
skipNextMutation,
skipPreviousMutation,
beginQueueSkip,
clearQueueSkip,
repeatEnabled,
queueLength,
]
)