fix: use audio.autoplay flag to unblock autoplay policy on chapter auto-advance
Some checks failed
CI / Lint (push) Has been cancelled
CI / Test (push) Has been cancelled
CI / Build (push) Has been cancelled

When the auto-next feature navigated to a new chapter and startAudio was
called on page load (outside a user gesture), the browser's autoplay policy
blocked the audio.play() call inside onCanPlay. Fix by setting
audio.autoplay = true before audio.load() when started automatically, then
resetting it in onCanPlay so manual pause/play is unaffected.
This commit is contained in:
Admin
2026-03-02 15:13:24 +05:00
parent 093e627a0e
commit 1536c7dad1

View File

@@ -2871,7 +2871,7 @@ const chapterTmpl = `
if (!activePara || activePara !== paras[idx]) highlightPara(idx); if (!activePara || activePara !== paras[idx]) highlightPara(idx);
} }
function onCanPlay() { if (stale()) { audio.removeEventListener('canplay', onCanPlay); return; } if (audio.paused) audio.play().then(setPlaying).catch(function(e){ setError(e.message); }); } function onCanPlay() { if (stale()) { audio.removeEventListener('canplay', onCanPlay); return; } audio.autoplay = false; if (audio.paused) audio.play().then(setPlaying).catch(function(e){ setError(e.message); }); }
function onWaiting() { if (!stale()) setStatus('Buffering\u2026'); } function onWaiting() { if (!stale()) setStatus('Buffering\u2026'); }
function onPlaying() { if (!stale()) setPlaying(); } function onPlaying() { if (!stale()) setPlaying(); }
function onPause() { if (!stale() && !audio.ended) setPaused(); } function onPause() { if (!stale() && !audio.ended) setPaused(); }
@@ -2921,11 +2921,12 @@ const chapterTmpl = `
navigator.mediaSession.setActionHandler('seekto', function(d) { if (isFinite(d.seekTime)) { audio.currentTime = d.seekTime; updateSeek(); } }); navigator.mediaSession.setActionHandler('seekto', function(d) { if (isFinite(d.seekTime)) { audio.currentTime = d.seekTime; updateSeek(); } });
} }
function startAudio() { function startAudio(autoplay) {
setGenerating(); setGenerating();
highlightPara(0); highlightPara(0);
generateAudio(CHAPTER_N, function (url) { generateAudio(CHAPTER_N, function (url) {
if (stale()) return; if (stale()) return;
if (autoplay) audio.autoplay = true;
audio.src = url; audio.load(); audio.src = url; audio.load();
registerMediaSession(); registerMediaSession();
}); });
@@ -2948,10 +2949,10 @@ const chapterTmpl = `
if (_autostart) { if (_autostart) {
try { localStorage.removeItem('tts_autostart'); } catch(_) {} try { localStorage.removeItem('tts_autostart'); } catch(_) {}
autoplayChk.checked = true; autoplayChk.checked = true;
setTimeout(startAudio, 100); setTimeout(function() { startAudio(true); }, 100);
} else if (new URLSearchParams(window.location.search).get('autoplay') === '1') { } else if (new URLSearchParams(window.location.search).get('autoplay') === '1') {
autoplayChk.checked = true; autoplayChk.checked = true;
setTimeout(startAudio, 100); setTimeout(function() { startAudio(true); }, 100);
} }
// ── double-tap left/right to navigate ──────────────────────────────────── // ── double-tap left/right to navigate ────────────────────────────────────