commit b342f43f72b47309f522c415b5e2e9452124ec7d
parent 44389a9627e7f890319da0c61f46113168edb51c
Author: Hunter
Date:   Tue,  4 Nov 2025 23:35:09 -0500

fix iOS safari bug where next song wouldn't reliably play if PWA was in background

Diffstat:
Mscript.js | 37+++++++++++++++++++++++++++++++++++++
1 file changed, 37 insertions(+), 0 deletions(-)

diff --git a/script.js b/script.js @@ -529,6 +529,38 @@ function startProgressBar() { } animate(); + + // iOS PWA background playback fix: use setInterval as backup + // setInterval is less throttled than requestAnimationFrame in background + backgroundPlaybackCheckInterval = setInterval(() => { + if (!audio.paused && audio.duration && audio.currentTime >= audio.duration - 0.5) { + console.log('Background check: song ended, triggering next'); + clearInterval(backgroundPlaybackCheckInterval); + backgroundPlaybackCheckInterval = null; + + if (songs[currentSongIndex].looping) { + audio.currentTime = 0; + audio.play(); + } else { + nextSong(); + } + } + + // Update Media Session position state for iOS + if ('mediaSession' in navigator && 'setPositionState' in navigator.mediaSession) { + try { + if (audio.duration && !isNaN(audio.duration) && isFinite(audio.duration)) { + navigator.mediaSession.setPositionState({ + duration: audio.duration, + playbackRate: audio.playbackRate, + position: audio.currentTime + }); + } + } catch (e) { + // Ignore errors from setPositionState + } + } + }, 500); // Check every 500ms } function stopProgressBar() { @@ -536,6 +568,10 @@ function stopProgressBar() { cancelAnimationFrame(animationFrameId); animationFrameId = null; } + if (backgroundPlaybackCheckInterval !== null) { + clearInterval(backgroundPlaybackCheckInterval); + backgroundPlaybackCheckInterval = null; + } } function resetProgressBar() { @@ -555,6 +591,7 @@ function updateProgressBar() { let isDragging = false; let wasPlayingBeforeDrag = false; let pendingSeekPercentage = null; +let backgroundPlaybackCheckInterval = null; function updateVisualProgress(event) { if (!playerReady) return;