fix: don't auto-resume audio on tab/app focus if user explicitly paused

This commit is contained in:
Honey committed 2026-05-09 16:17:50 +02:00
1 parent 8e752fa892
commit 2feb00be62
1 file changed
+7 -4
+7 -4
View File
@@ -162,6 +162,8 @@ window.addEventListener('load', () => {
S.audio.volume = 0.5;
let _wasPlayingBeforeHide = false;
function _onForeground() {
const s = S.queue[S.qIdx];
if(!s) return;
@@ -170,14 +172,15 @@ window.addEventListener('load', () => {
// Skip when native is active: Media3 owns the system media session.
if(navigator.mediaSession && !S._trackChanging && !window.NSBridge)
navigator.mediaSession.playbackState = S.audio.paused ? 'paused' : 'playing';
// If audio was playing before backgrounding but is now paused (OS kill), try to resume.
// No-op when native (S.audio.src is empty; native handles its own recovery).
// Only auto-resume if audio was actually playing when the page hid (OS-killed buffer).
// If the user explicitly paused, _wasPlayingBeforeHide is false — don't restart.
if(!S.audio.paused) return;
if(s && S.audio.src) S.audio.play().catch(() => { S._trackChanging = false; });
if(s && S.audio.src && _wasPlayingBeforeHide) S.audio.play().catch(() => { S._trackChanging = false; });
}
document.addEventListener('visibilitychange', () => {
if(document.visibilityState === 'visible') _onForeground();
if(document.visibilityState === 'hidden') _wasPlayingBeforeHide = !S.audio.paused;
else if(document.visibilityState === 'visible') _onForeground();
});
// pageshow fires more reliably than visibilitychange on iOS PWA and Samsung
window.addEventListener('pageshow', e => { if(e.persisted) _onForeground(); });