Compare commits

..
2 changed files with 21 additions and 5 deletions

No files matched your search

+20 -5
View File
@@ -2,6 +2,7 @@ import { dolbyAtmosSupported } from "@/lib/playback-capabilities"
import {
createAudioPlaylist,
setAudioModeAsync,
setIsAudioActiveAsync,
type AudioMetadata,
type AudioPlaylist,
type AudioPlaylistStatus,
@@ -375,7 +376,8 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
const handlePlaylistStatusUpdate = useCallback(
(status: AudioPlaylistStatus) => {
if (!status.isLoaded) return
// iOS may remove the finished item before delivering its final status.
if (!status.isLoaded && !status.didJustFinish) return
const state = storeRef.current.getState()
if (loadedSongIdsRef.current[status.currentIndex] !== state.queue[0]) {
return
@@ -426,7 +428,14 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
isPlayingRef.current = false
setIsPlayingState(false)
storeRef.current.update(playbackQueue.skipNext)
void playlistRef.current?.seekTo(0)
const playlist = playlistRef.current
if (playlist) {
playlist.pause()
// Reload the final item, which iOS removes when playback ends.
ignoreTrackChangeRef.current = true
playlist.skipTo(status.currentIndex)
scheduleTrackChangeHandling()
}
currentTimeRef.current = 0
setCurrentTime(0)
return
@@ -442,7 +451,7 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
playlist?.play()
}
},
[]
[scheduleTrackChangeHandling]
)
const handleTrackChanged = useCallback(
@@ -800,9 +809,15 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
setIsPlayingState(true)
playlist.play()
} else {
// Activating the session here would stop other apps' audio.
// `play()` activates it (iOS) and requests focus (Android).
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.")
}
}
}
}
@@ -955,6 +955,7 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
if (!shouldContinuePlaying) {
autoPlayNextRef.current = false
isPlayingRef.current = false
audio.pause()
audio.currentTime = 0
currentTimeRef.current = 0
setCurrentTime(0)