fix(mobile): pause playback from native & duplicate audio (#7)
Co-authored-by: Rem Helper <remhelper@abunchofknowitalls.com> Reviewed-on: https://git.abunchofknowitalls.com/joshuafhiggins/otunes/pulls/7
This commit is contained in:
1 parent
103b6fda79
commit
6eb8520025
1 file changed
+50
-12
@@ -33,6 +33,12 @@ 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
|
||||
@@ -129,6 +135,8 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
|
||||
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)
|
||||
|
||||
const [isPlaying, setIsPlayingState] = useState(false)
|
||||
const [currentTime, setCurrentTime] = useState(0)
|
||||
@@ -180,8 +188,10 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
|
||||
const destroyPlayer = useCallback(() => {
|
||||
statusSubscriptionRef.current?.remove()
|
||||
statusSubscriptionRef.current = null
|
||||
playerRef.current?.setActiveForLockScreen(false)
|
||||
playerRef.current?.remove()
|
||||
const player = playerRef.current
|
||||
player?.pause()
|
||||
player?.setActiveForLockScreen(false)
|
||||
player?.remove()
|
||||
playerRef.current = null
|
||||
loadedSongKeyRef.current = null
|
||||
loadedSongIdRef.current = null
|
||||
@@ -205,19 +215,30 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
|
||||
setCurrentTime(seekSeconds)
|
||||
}
|
||||
|
||||
if (
|
||||
(isPlayingRef.current || autoPlayNextRef.current) &&
|
||||
status.isLoaded &&
|
||||
!status.playing &&
|
||||
!status.didJustFinish
|
||||
if (status.playing) {
|
||||
autoPlayNextRef.current = false
|
||||
if (!isPlayingRef.current) {
|
||||
isPlayingRef.current = true
|
||||
setIsPlayingState(true)
|
||||
}
|
||||
} else if (
|
||||
isPlayingRef.current &&
|
||||
!status.didJustFinish &&
|
||||
statusLooksPaused(status)
|
||||
) {
|
||||
playerRef.current?.play()
|
||||
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
|
||||
return
|
||||
}
|
||||
|
||||
@@ -251,6 +272,9 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
|
||||
isPlayingRef.current = false
|
||||
setIsPlayingState(false)
|
||||
})
|
||||
.finally(() => {
|
||||
handlingEndRef.current = false
|
||||
})
|
||||
}
|
||||
}, [])
|
||||
|
||||
@@ -344,6 +368,7 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
|
||||
prevSongIdRef.current = null
|
||||
loadedSongKeyRef.current = null
|
||||
loadedSongIdRef.current = null
|
||||
destroyPlayer()
|
||||
return
|
||||
}
|
||||
|
||||
@@ -362,8 +387,9 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
|
||||
lastSavedProgressRef.current = -1
|
||||
if (!isInitialLoad) {
|
||||
pendingSongChangeRef.current = true
|
||||
destroyPlayer()
|
||||
}
|
||||
}, [currentSong?.id])
|
||||
}, [currentSong?.id, destroyPlayer])
|
||||
|
||||
useEffect(() => {
|
||||
if (!currentSong || !playback) return
|
||||
@@ -397,6 +423,11 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
|
||||
}, [isPlaying, currentSong?.id, saveProgress])
|
||||
|
||||
useEffect(() => {
|
||||
const loadRequestId = ++loadRequestIdRef.current
|
||||
const isCurrentLoad = () =>
|
||||
loadRequestIdRef.current === loadRequestId &&
|
||||
currentSongIdRef.current === currentSong?.id
|
||||
|
||||
async function loadSong() {
|
||||
if (!config || !currentSong || user === undefined || !user) {
|
||||
if (!currentSong) {
|
||||
@@ -408,7 +439,7 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
|
||||
const song = await convex.query(api.library.getSong, {
|
||||
id: currentSong.id,
|
||||
})
|
||||
if (!song || currentSongIdRef.current !== currentSong.id) {
|
||||
if (!song || !isCurrentLoad()) {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -429,7 +460,7 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
|
||||
return
|
||||
}
|
||||
|
||||
if (currentSongIdRef.current !== currentSong.id) {
|
||||
if (!isCurrentLoad()) {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -442,6 +473,13 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
|
||||
{ cookieHeader, songTitle: currentSong.title }
|
||||
)
|
||||
|
||||
const shouldPlayAfterLoad = isPlayingRef.current || autoPlayNextRef.current
|
||||
|
||||
destroyPlayer()
|
||||
if (!isCurrentLoad()) {
|
||||
return
|
||||
}
|
||||
|
||||
const player = ensurePlayer()
|
||||
loadedSongKeyRef.current = loadKey
|
||||
loadedSongIdRef.current = currentSong.id
|
||||
@@ -454,7 +492,7 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
|
||||
{ showSeekForward: true, showSeekBackward: true }
|
||||
)
|
||||
|
||||
if (isPlayingRef.current || autoPlayNextRef.current) {
|
||||
if (shouldPlayAfterLoad) {
|
||||
player.play()
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user