forked from melcor76/js-tetris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsound.js
63 lines (54 loc) · 1.48 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
export default class Sound {
constructor(parent){
this.parent = parent;
this.sounds = [];
this.muted = true;
}
create(src, id, loop = false){
let audio = document.createElement("audio");
audio.src = src;
audio.id = id;
audio.muted = true;
this.sounds.push(audio);
this.parent.append(audio);
if(loop){
audio.setAttribute("loop", "")
}
return audio;
}
}
Sound.prototype.soundSetting = function(){
let soundItems = document.querySelectorAll(".sound-item");
for(let soundItem of soundItems){
soundItem.addEventListener("click", (e)=>{
this.muteToggle();
});
}
};
Sound.prototype.muteToggle = function(){
if(!this.muted){
for(let sound of this.sounds){
sound.muted = true;
}
document.querySelector("#sound-speaker").innerHTML = "\u{1F507}";
document.querySelector("#sound-description").innerHTML = "off";
this.muted = true;
}else{
for(let sound of this.sounds){
sound.muted = false;
}
document.querySelector("#sound-speaker").innerHTML = "\u{1F509}";
document.querySelector("#sound-description").innerHTML = "on";
this.muted = false;
}
};
Sound.prototype.pause = function(){
for(let sound of this.sounds){
sound.pause();
}
}
Sound.prototype.play = function(){
for(let sound of this.sounds){
sound.play();
}
}