-
Notifications
You must be signed in to change notification settings - Fork 0
/
sound-lib.js
52 lines (45 loc) · 931 Bytes
/
sound-lib.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
46
47
48
49
50
51
52
var soundDict = {};
var musicDict = {};
let activeMusic = null;
var soundOn = true;
function playSound(fileName, volume) {
if (!soundOn) {
return;
}
var audio = soundDict[fileName];
if (audio == null) {
soundDict[fileName] = new Audio("/sounds/" + fileName);
audio = soundDict[fileName];
}
if (typeof volume === Number) {
audio.volume = volume;
}
audio.play();
}
function playMusic(fileName, volume) {
if (!soundOn) {
return;
}
if (activeMusic != null) {
activeMusic.pause();
activeMusic.currentTime = 0;
}
activeMusic = musicDict[fileName];
if (activeMusic == null) {
musicDict[fileName] = new Audio("/music/" + fileName);
activeMusic = musicDict[fileName];
}
if (typeof volume === Number) {
activeMusic.volume = volume;
}
activeMusic.play();
}
function stopMusic() {
if (!soundOn) {
return;
}
if (activeMusic != null) {
activeMusic.pause();
activeMusic.currentTime = 0;
}
}