Queue message sound while audio is priming

This commit is contained in:
Your Name
2026-09-30 17:05:32 +08:00
parent 06df51ca08
commit dd77ba903b
2 changed files with 57 additions and 1 deletions
+11 -1
View File
@@ -1,5 +1,6 @@
let audio: HTMLAudioElement | null = null;
let priming = false;
let pendingMessagePlay = false;
async function primeSound(): Promise<void> {
if (priming) return;
@@ -14,9 +15,14 @@ async function primeSound(): Promise<void> {
window.removeEventListener('pointerdown', primeSound);
window.removeEventListener('keydown', primeSound);
} catch {
try { if (audio) audio.muted = false; } catch { /* Audio may be unavailable. */ }
// Keep gesture listeners so the next gesture can retry.
} finally {
try { if (audio) audio.muted = false; } catch { /* Audio may be unavailable. */ }
priming = false;
if (pendingMessagePlay) {
pendingMessagePlay = false;
playMessageNotificationSound();
}
}
}
@@ -24,6 +30,10 @@ window.addEventListener('pointerdown', primeSound);
window.addEventListener('keydown', primeSound);
export function playMessageNotificationSound(): void {
if (priming) {
pendingMessagePlay = true;
return;
}
try {
audio ??= new Audio('/message-notification.mp3');
audio.currentTime = 0;