fix: don't auto-resume audio on tab/app focus if user explicitly paused
This commit is contained in:
1 parent
8e752fa892
commit
2feb00be62
1 file changed
+7
-4
+7
-4
@@ -162,6 +162,8 @@ window.addEventListener('load', () => {
|
|||||||
|
|
||||||
S.audio.volume = 0.5;
|
S.audio.volume = 0.5;
|
||||||
|
|
||||||
|
let _wasPlayingBeforeHide = false;
|
||||||
|
|
||||||
function _onForeground() {
|
function _onForeground() {
|
||||||
const s = S.queue[S.qIdx];
|
const s = S.queue[S.qIdx];
|
||||||
if(!s) return;
|
if(!s) return;
|
||||||
@@ -170,14 +172,15 @@ window.addEventListener('load', () => {
|
|||||||
// Skip when native is active: Media3 owns the system media session.
|
// Skip when native is active: Media3 owns the system media session.
|
||||||
if(navigator.mediaSession && !S._trackChanging && !window.NSBridge)
|
if(navigator.mediaSession && !S._trackChanging && !window.NSBridge)
|
||||||
navigator.mediaSession.playbackState = S.audio.paused ? 'paused' : 'playing';
|
navigator.mediaSession.playbackState = S.audio.paused ? 'paused' : 'playing';
|
||||||
// If audio was playing before backgrounding but is now paused (OS kill), try to resume.
|
// Only auto-resume if audio was actually playing when the page hid (OS-killed buffer).
|
||||||
// No-op when native (S.audio.src is empty; native handles its own recovery).
|
// If the user explicitly paused, _wasPlayingBeforeHide is false — don't restart.
|
||||||
if(!S.audio.paused) return;
|
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', () => {
|
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
|
// pageshow fires more reliably than visibilitychange on iOS PWA and Samsung
|
||||||
window.addEventListener('pageshow', e => { if(e.persisted) _onForeground(); });
|
window.addEventListener('pageshow', e => { if(e.persisted) _onForeground(); });
|
||||||
|
|||||||
Reference in new issue
Block a user