-
Notifications
You must be signed in to change notification settings - Fork 0
/
sound.js
45 lines (39 loc) · 1.15 KB
/
sound.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { SYMBOLS, AUDIO_FILES, SVG_FILES } from "./constants";
// Play audio for various states
export const playAudio = (state, audioType) => {
if (!state.audioEnabled) return;
let audioFile;
switch (audioType) {
case "gameMove":
audioFile =
state.currentSymbol === SYMBOLS.player1
? AUDIO_FILES.toggleOn
: AUDIO_FILES.toggleOff;
break;
case "win":
audioFile = AUDIO_FILES.win;
break;
case "draw":
audioFile = AUDIO_FILES.draw;
break;
case "toggleOn":
audioFile = AUDIO_FILES.toggleOn;
break;
default:
console.warn(`Invalid audioType: ${audioType}`);
return;
}
const audio = document.createElement("audio");
audio.src = audioFile;
audio.play();
};
// Check if user insists on audio
export const setupAudio = (state) => {
const audioSelector = document.querySelector("#audio");
audioSelector.src = SVG_FILES.on;
audioSelector.addEventListener("click", () => {
state.audioEnabled = !state.audioEnabled;
audioSelector.src = state.audioEnabled ? SVG_FILES.on : SVG_FILES.off;
if (state.audioEnabled) playAudio(state, "toggleOn");
});
};