fix: always autoplay after generation and resume TTS on HTMX chapter swap
Some checks failed
CI / Lint (push) Has been cancelled
CI / Test (push) Has been cancelled
CI / Build (push) Has been cancelled

This commit is contained in:
Admin
2026-03-02 19:43:48 +05:00
parent 9388a355e5
commit f28de2195d

View File

@@ -2927,7 +2927,12 @@ const chapterTmpl = `
highlightPara(0);
generateAudio(CHAPTER_N, function (url) {
if (stale()) return;
if (autoplay) audio.autoplay = true;
// Always set autoplay=true before load(): the browser needs this hint set
// synchronously before load() regardless of whether we got here via a user
// gesture or a programmatic trigger (e.g. chapter transition). The canplay
// handler will call play() as well, but autoplay on the element is the only
// reliable path after an async fetch where the gesture stack is gone.
audio.autoplay = true;
audio.src = url; audio.load();
audio.playbackRate = getSpeed();
registerMediaSession();
@@ -2943,7 +2948,15 @@ const chapterTmpl = `
startAudio();
};
window.__ttsBeforeSwap = function () { window.__ttsGen++; stop(); };
window.__ttsBeforeSwap = function () {
// If TTS is active (generating or has audio loaded), persist the autostart
// flag so the incoming chapter's init code will restart TTS automatically.
var wasActive = currentAudioCtrl !== null || (audio.src && !audio.ended);
if (wasActive) {
try { localStorage.setItem('tts_autostart', '1'); } catch(_) {}
}
window.__ttsGen++; stop();
};
document.body.addEventListener('htmx:beforeSwap', window.__ttsBeforeSwap);
var _autostart = false;