Skip to content

Commit add9341

Browse files
dorlugasigalCopilot
andcommitted
fix(ui): reuse AudioContext for mobile notification sound
Mobile browsers require AudioContext to be created or resumed during a user gesture. Create it once on the first click/touch and reuse it for all subsequent notification sounds. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent fbed57f commit add9341

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

public/terminal.html

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3134,9 +3134,21 @@ <h3>
31343134
const originalTitle = document.title;
31353135
let hasUnread = false;
31363136

3137+
// Shared AudioContext — must be created/resumed on user gesture for mobile
3138+
let notifAudioCtx = null;
3139+
function ensureAudioContext() {
3140+
if (!notifAudioCtx) {
3141+
notifAudioCtx = new (window.AudioContext || window.webkitAudioContext)();
3142+
}
3143+
if (notifAudioCtx.state === 'suspended') notifAudioCtx.resume();
3144+
return notifAudioCtx;
3145+
}
3146+
document.addEventListener('click', ensureAudioContext, { once: true });
3147+
document.addEventListener('touchstart', ensureAudioContext, { once: true });
3148+
31373149
function playNotificationSound() {
31383150
try {
3139-
const ctx = new (window.AudioContext || window.webkitAudioContext)();
3151+
const ctx = ensureAudioContext();
31403152
const osc = ctx.createOscillator();
31413153
const gain = ctx.createGain();
31423154
osc.connect(gain);
@@ -3147,7 +3159,6 @@ <h3>
31473159
gain.gain.exponentialRampToValueAtTime(0.001, ctx.currentTime + 0.15);
31483160
osc.start(ctx.currentTime);
31493161
osc.stop(ctx.currentTime + 0.15);
3150-
osc.onended = () => ctx.close();
31513162
} catch {
31523163
// Audio not available
31533164
}

0 commit comments

Comments
 (0)