From 260139b7a3f0ccef2379ebc7e664df014f0d1a79 Mon Sep 17 00:00:00 2001 From: James Ketrenos Date: Mon, 13 Oct 2025 14:08:00 -0700 Subject: [PATCH] Fixed audio disabling so it remains quiet --- client/src/RoomView.tsx | 128 ++++++++++++++++++++++++++-------------- 1 file changed, 83 insertions(+), 45 deletions(-) diff --git a/client/src/RoomView.tsx b/client/src/RoomView.tsx index aa57b01..ed83125 100644 --- a/client/src/RoomView.tsx +++ b/client/src/RoomView.tsx @@ -225,13 +225,26 @@ const RoomView = (props: RoomProps) => { useEffect(() => { if (state === "volcano") { - if (!audioEffects.volcano) { - audioEffects.volcano = loadAudio("volcano-eruption.mp3"); - audioEffects.volcano.volume = volume * volume; + if (audio) { + if (!audioEffects.volcano) { + audioEffects.volcano = loadAudio("volcano-eruption.mp3"); + audioEffects.volcano.volume = volume * volume; + } else { + if (!audioEffects.volcano.hasPlayed && audioEffects.volcano.readyState >= 2) { + audioEffects.volcano.hasPlayed = true; + audioEffects.volcano.play().catch((e) => console.error("Audio play failed:", e)); + } + } } else { - if (!audioEffects.volcano.hasPlayed && audioEffects.volcano.readyState >= 2) { - audioEffects.volcano.hasPlayed = true; - audioEffects.volcano.play().catch((e) => console.error("Audio play failed:", e)); + // Audio disabled -> stop any currently playing volcano effect + if (audioEffects.volcano) { + try { + audioEffects.volcano.pause(); + audioEffects.volcano.currentTime = 0; + } catch (e) { + /* ignore */ + } + audioEffects.volcano.hasPlayed = false; } } } else { @@ -242,58 +255,83 @@ const RoomView = (props: RoomProps) => { }, [state, volume]); useEffect(() => { - if (turn && turn.color === color && state !== "room") { - if (!audioEffects.yourTurn) { - audioEffects.yourTurn = loadAudio("its-your-turn.mp3"); - audioEffects.yourTurn.volume = volume * volume; - } else { - if (!audioEffects.yourTurn.hasPlayed && audioEffects.yourTurn.readyState >= 2) { - audioEffects.yourTurn.hasPlayed = true; - audioEffects.yourTurn.play().catch((e) => console.error("Audio play failed:", e)); + // When audio is enabled we may create/play effects; when disabled ensure + // any existing effects are stopped and reset. + if (audio) { + if (turn && turn.color === color && state !== "room") { + if (!audioEffects.yourTurn) { + audioEffects.yourTurn = loadAudio("its-your-turn.mp3"); + audioEffects.yourTurn.volume = volume * volume; + } else { + if (!audioEffects.yourTurn.hasPlayed && audioEffects.yourTurn.readyState >= 2) { + audioEffects.yourTurn.hasPlayed = true; + audioEffects.yourTurn.play().catch((e) => console.error("Audio play failed:", e)); + } + } + } else if (turn) { + if (audioEffects.yourTurn) { + audioEffects.yourTurn.hasPlayed = false; } } - } else if (turn) { - if (audioEffects.yourTurn) { - audioEffects.yourTurn.hasPlayed = false; - } - } - if (turn && turn.roll === 7) { - if (!audioEffects.robber) { - audioEffects.robber = loadAudio("robber.mp3"); - audioEffects.robber.volume = volume * volume; - } else { - if (!audioEffects.robber.hasPlayed && audioEffects.robber.readyState >= 2) { - audioEffects.robber.hasPlayed = true; - audioEffects.robber.play().catch((e) => console.error("Audio play failed:", e)); + if (turn && turn.roll === 7) { + if (!audioEffects.robber) { + audioEffects.robber = loadAudio("robber.mp3"); + audioEffects.robber.volume = volume * volume; + } else { + if (!audioEffects.robber.hasPlayed && audioEffects.robber.readyState >= 2) { + audioEffects.robber.hasPlayed = true; + audioEffects.robber.play().catch((e) => console.error("Audio play failed:", e)); + } + } + } else if (turn) { + if (audioEffects.robber) { + audioEffects.robber.hasPlayed = false; } } - } else if (turn) { - if (audioEffects.robber) { - audioEffects.robber.hasPlayed = false; - } - } - if (turn && turn.actions && turn.actions.indexOf("playing-knight") !== -1) { - if (!audioEffects.knights) { - audioEffects.knights = loadAudio("the-knights-who-say-ni.mp3"); - audioEffects.knights.volume = volume * volume; - } else { - if (!audioEffects.knights.hasPlayed && audioEffects.knights.readyState >= 2) { - audioEffects.knights.hasPlayed = true; - audioEffects.knights.play().catch((e) => console.error("Audio play failed:", e)); + if (turn && turn.actions && turn.actions.indexOf("playing-knight") !== -1) { + if (!audioEffects.knights) { + audioEffects.knights = loadAudio("the-knights-who-say-ni.mp3"); + audioEffects.knights.volume = volume * volume; + } else { + if (!audioEffects.knights.hasPlayed && audioEffects.knights.readyState >= 2) { + audioEffects.knights.hasPlayed = true; + audioEffects.knights.play().catch((e) => console.error("Audio play failed:", e)); + } + } + } else if (turn && turn.actions && turn.actions.indexOf("playing-knight") === -1) { + if (audioEffects.knights) { + audioEffects.knights.hasPlayed = false; } } - } else if (turn && turn.actions && turn.actions.indexOf("playing-knight") === -1) { - if (audioEffects.knights) { - audioEffects.knights.hasPlayed = false; - } + } else { + // audio disabled: stop any currently playing effects and reset their state + const stopIfPlaying = (ae?: AudioEffect) => { + if (!ae) return; + try { + ae.pause(); + ae.currentTime = 0; + } catch (e) { + /* ignore */ + } + ae.hasPlayed = false; + }; + stopIfPlaying(audioEffects.yourTurn); + stopIfPlaying(audioEffects.robber); + stopIfPlaying(audioEffects.knights); } }, [state, turn, color, volume]); useEffect(() => { for (const key in audioEffects) { - audioEffects[key].volume = volume * volume; + if (audioEffects[key]) { + try { + audioEffects[key]!.volume = volume * volume; + } catch (e) { + /* ignore */ + } + } } }, [volume]);