diff options
| author | David Skoland <davidskoland@gmail.com> | 2026-02-07 13:10:27 +0100 |
|---|---|---|
| committer | David Skoland <davidskoland@gmail.com> | 2026-02-07 13:10:27 +0100 |
| commit | 0c76b200cc68c59772df930acd34a58bd6272c7f (patch) | |
| tree | 173b1362585945d734f87c9808c618ec55a3d553 | |
| parent | 4ab187b991ec40993c4b030e1612d9bb41f18924 (diff) | |
Catch rejected play() promises from browser autoplay policy
Browsers reject .play() with a DOMException when the user hasn't
interacted with the document yet. Add .catch(() => {}) to all 9
play() call sites to suppress the uncaught promise rejection.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
| -rw-r--r-- | webAO/client/checkCallword.ts | 2 | ||||
| -rw-r--r-- | webAO/packets/handlers/handleMC.ts | 2 | ||||
| -rw-r--r-- | webAO/packets/handlers/handleRMC.ts | 2 | ||||
| -rw-r--r-- | webAO/packets/handlers/handleZZ.ts | 2 | ||||
| -rw-r--r-- | webAO/viewport/utils/handleICSpeaking.ts | 2 | ||||
| -rw-r--r-- | webAO/viewport/utils/initTestimonyUpdater.ts | 2 | ||||
| -rw-r--r-- | webAO/viewport/viewport.ts | 6 |
7 files changed, 9 insertions, 9 deletions
diff --git a/webAO/client/checkCallword.ts b/webAO/client/checkCallword.ts index b7413f7..795eac9 100644 --- a/webAO/client/checkCallword.ts +++ b/webAO/client/checkCallword.ts @@ -11,7 +11,7 @@ export function checkCallword(message: string, sfxAudio: HTMLAudioElement) { if (item !== "" && message.toLowerCase().includes(item.toLowerCase())) { sfxAudio.pause(); sfxAudio.src = `${AO_HOST}sounds/general/sfx-gallery.opus`; - sfxAudio.play(); + sfxAudio.play().catch(() => {}); } } } diff --git a/webAO/packets/handlers/handleMC.ts b/webAO/packets/handlers/handleMC.ts index 8a1c0a2..44237ea 100644 --- a/webAO/packets/handlers/handleMC.ts +++ b/webAO/packets/handlers/handleMC.ts @@ -24,7 +24,7 @@ export const handleMC = (args: string[]) => { music.src = `${AO_HOST}sounds/music/${encodeURI(track.toLowerCase())}`; } music.loop = looping; - music.play(); + music.play().catch(() => {}); try { musicname = client.chars[charID].name; diff --git a/webAO/packets/handlers/handleRMC.ts b/webAO/packets/handlers/handleRMC.ts index 7758844..0cc6aa7 100644 --- a/webAO/packets/handlers/handleRMC.ts +++ b/webAO/packets/handlers/handleRMC.ts @@ -17,7 +17,7 @@ export const handleRMC = (args: string[]) => { music.currentTime += parseFloat( music.totime + (new Date().getTime() / 1000 - music.offset), ).toFixed(3); - music.play(); + music.play().catch(() => {}); }, false, ); diff --git a/webAO/packets/handlers/handleZZ.ts b/webAO/packets/handlers/handleZZ.ts index 31ceef5..0496d42 100644 --- a/webAO/packets/handlers/handleZZ.ts +++ b/webAO/packets/handlers/handleZZ.ts @@ -18,6 +18,6 @@ export const handleZZ = (args: string[]) => { const oldvolume = client.viewport.getSfxAudio().volume; client.viewport.getSfxAudio().volume = 1; client.viewport.getSfxAudio().src = `${AO_HOST}sounds/general/sfx-gallery.opus`; - client.viewport.getSfxAudio().play(); + client.viewport.getSfxAudio().play().catch(() => {}); client.viewport.getSfxAudio().volume = oldvolume; }; diff --git a/webAO/viewport/utils/handleICSpeaking.ts b/webAO/viewport/utils/handleICSpeaking.ts index 6cf665c..ec791c5 100644 --- a/webAO/viewport/utils/handleICSpeaking.ts +++ b/webAO/viewport/utils/handleICSpeaking.ts @@ -169,7 +169,7 @@ export const handle_ic_speaking = async (playerChatMsg: ChatMsg) => { )}/${shout}.opus`; const exists = await fileExists(perCharPath); client.viewport.shoutaudio.src = exists ? perCharPath : client.resources[shout].sfx; - client.viewport.shoutaudio.play(); + client.viewport.shoutaudio.play().catch(() => {}); client.viewport.setShoutTimer(client.resources[shout].duration); } else { client.viewport.setShoutTimer(0); diff --git a/webAO/viewport/utils/initTestimonyUpdater.ts b/webAO/viewport/utils/initTestimonyUpdater.ts index c1e4d93..a545c97 100644 --- a/webAO/viewport/utils/initTestimonyUpdater.ts +++ b/webAO/viewport/utils/initTestimonyUpdater.ts @@ -18,7 +18,7 @@ export const initTestimonyUpdater = () => { } client.viewport.testimonyAudio.src = client.resources[testimony].sfx; - client.viewport.testimonyAudio.play(); + client.viewport.testimonyAudio.play().catch(() => {}); const testimonyOverlay = <HTMLImageElement>( document.getElementById("client_testimony") diff --git a/webAO/viewport/viewport.ts b/webAO/viewport/viewport.ts index c03d750..a7363a9 100644 --- a/webAO/viewport/viewport.ts +++ b/webAO/viewport/viewport.ts @@ -104,7 +104,7 @@ const viewport = (): Viewport => { sfxAudio.pause(); sfxAudio.loop = looping; sfxAudio.src = sfxname; - sfxAudio.play(); + sfxAudio.play().catch(() => {}); }; /** * Updates the testimony overaly @@ -150,7 +150,7 @@ const viewport = (): Viewport => { const charEmote = chatmsg.sprite.toLowerCase(); if (chatmsg.content.charAt(textnow.length) !== " ") { - blipChannels[currentBlipChannel].play(); + blipChannels[currentBlipChannel].play().catch(() => {}); currentBlipChannel++; currentBlipChannel %= blipChannels.length; } @@ -356,7 +356,7 @@ const viewport = (): Viewport => { eviBox.style.opacity = "1"; testimonyAudio.src = `${AO_HOST}sounds/general/sfx-evidenceshoop.opus`; - testimonyAudio.play(); + testimonyAudio.play().catch(() => {}); if (chatmsg.side === "def") { // Only def show evidence on right |
