Fixed audio disabling so it remains quiet
This commit is contained in:
parent
b541dd49e2
commit
260139b7a3
@ -225,13 +225,26 @@ const RoomView = (props: RoomProps) => {
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (state === "volcano") {
|
if (state === "volcano") {
|
||||||
if (!audioEffects.volcano) {
|
if (audio) {
|
||||||
audioEffects.volcano = loadAudio("volcano-eruption.mp3");
|
if (!audioEffects.volcano) {
|
||||||
audioEffects.volcano.volume = volume * volume;
|
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 {
|
} else {
|
||||||
if (!audioEffects.volcano.hasPlayed && audioEffects.volcano.readyState >= 2) {
|
// Audio disabled -> stop any currently playing volcano effect
|
||||||
audioEffects.volcano.hasPlayed = true;
|
if (audioEffects.volcano) {
|
||||||
audioEffects.volcano.play().catch((e) => console.error("Audio play failed:", e));
|
try {
|
||||||
|
audioEffects.volcano.pause();
|
||||||
|
audioEffects.volcano.currentTime = 0;
|
||||||
|
} catch (e) {
|
||||||
|
/* ignore */
|
||||||
|
}
|
||||||
|
audioEffects.volcano.hasPlayed = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -242,58 +255,83 @@ const RoomView = (props: RoomProps) => {
|
|||||||
}, [state, volume]);
|
}, [state, volume]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (turn && turn.color === color && state !== "room") {
|
// When audio is enabled we may create/play effects; when disabled ensure
|
||||||
if (!audioEffects.yourTurn) {
|
// any existing effects are stopped and reset.
|
||||||
audioEffects.yourTurn = loadAudio("its-your-turn.mp3");
|
if (audio) {
|
||||||
audioEffects.yourTurn.volume = volume * volume;
|
if (turn && turn.color === color && state !== "room") {
|
||||||
} else {
|
if (!audioEffects.yourTurn) {
|
||||||
if (!audioEffects.yourTurn.hasPlayed && audioEffects.yourTurn.readyState >= 2) {
|
audioEffects.yourTurn = loadAudio("its-your-turn.mp3");
|
||||||
audioEffects.yourTurn.hasPlayed = true;
|
audioEffects.yourTurn.volume = volume * volume;
|
||||||
audioEffects.yourTurn.play().catch((e) => console.error("Audio play failed:", e));
|
} 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 (turn && turn.roll === 7) {
|
||||||
if (!audioEffects.robber) {
|
if (!audioEffects.robber) {
|
||||||
audioEffects.robber = loadAudio("robber.mp3");
|
audioEffects.robber = loadAudio("robber.mp3");
|
||||||
audioEffects.robber.volume = volume * volume;
|
audioEffects.robber.volume = volume * volume;
|
||||||
} else {
|
} else {
|
||||||
if (!audioEffects.robber.hasPlayed && audioEffects.robber.readyState >= 2) {
|
if (!audioEffects.robber.hasPlayed && audioEffects.robber.readyState >= 2) {
|
||||||
audioEffects.robber.hasPlayed = true;
|
audioEffects.robber.hasPlayed = true;
|
||||||
audioEffects.robber.play().catch((e) => console.error("Audio play failed:", e));
|
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 (turn && turn.actions && turn.actions.indexOf("playing-knight") !== -1) {
|
||||||
if (!audioEffects.knights) {
|
if (!audioEffects.knights) {
|
||||||
audioEffects.knights = loadAudio("the-knights-who-say-ni.mp3");
|
audioEffects.knights = loadAudio("the-knights-who-say-ni.mp3");
|
||||||
audioEffects.knights.volume = volume * volume;
|
audioEffects.knights.volume = volume * volume;
|
||||||
} else {
|
} else {
|
||||||
if (!audioEffects.knights.hasPlayed && audioEffects.knights.readyState >= 2) {
|
if (!audioEffects.knights.hasPlayed && audioEffects.knights.readyState >= 2) {
|
||||||
audioEffects.knights.hasPlayed = true;
|
audioEffects.knights.hasPlayed = true;
|
||||||
audioEffects.knights.play().catch((e) => console.error("Audio play failed:", e));
|
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) {
|
} else {
|
||||||
if (audioEffects.knights) {
|
// audio disabled: stop any currently playing effects and reset their state
|
||||||
audioEffects.knights.hasPlayed = false;
|
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]);
|
}, [state, turn, color, volume]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
for (const key in audioEffects) {
|
for (const key in audioEffects) {
|
||||||
audioEffects[key].volume = volume * volume;
|
if (audioEffects[key]) {
|
||||||
|
try {
|
||||||
|
audioEffects[key]!.volume = volume * volume;
|
||||||
|
} catch (e) {
|
||||||
|
/* ignore */
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}, [volume]);
|
}, [volume]);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user