From f3572c646a1dc5af05fa04b08bcb2f09675dbbbe Mon Sep 17 00:00:00 2001 From: you Date: Sun, 22 Mar 2026 17:27:00 +0000 Subject: [PATCH] Audio: unlock on first user gesture after restore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When audio was previously enabled, registers one-shot click/touch/key listener to init AudioContext on first interaction. Any tap on the page is enough — no need to toggle the checkbox. --- public/audio.js | 15 +++++++++++++-- public/index.html | 2 +- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/public/audio.js b/public/audio.js index c5ee8344..e573b19d 100644 --- a/public/audio.js +++ b/public/audio.js @@ -171,11 +171,22 @@ const savedBpm = localStorage.getItem('live-audio-bpm'); if (savedBpm) bpm = parseInt(savedBpm, 10) || 120; const savedVol = localStorage.getItem('live-audio-volume'); - // Don't create AudioContext here — no user gesture yet, browser will suspend it. - // Just store the desired volume; initAudio() will apply it when user interacts. if (savedVol) _pendingVolume = parseFloat(savedVol) || 0.3; const savedVoice = localStorage.getItem('live-audio-voice'); if (savedVoice) setVoice(savedVoice); + + // If audio was enabled, wait for ANY user gesture to init context + if (audioEnabled) { + const unlockAudio = () => { + initAudio(); + document.removeEventListener('click', unlockAudio, true); + document.removeEventListener('touchstart', unlockAudio, true); + document.removeEventListener('keydown', unlockAudio, true); + }; + document.addEventListener('click', unlockAudio, true); + document.addEventListener('touchstart', unlockAudio, true); + document.addEventListener('keydown', unlockAudio, true); + } } // Export engine + helpers for voice modules diff --git a/public/index.html b/public/index.html index d675e5e7..8e639bf5 100644 --- a/public/index.html +++ b/public/index.html @@ -90,7 +90,7 @@ - +