-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsound.js
39 lines (39 loc) · 1.02 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
class Sound{
#state=false;
constructor(src) {
this.sounds = {};
}
setState = (state) => {
this.#state=state;
};
add = (key,src) =>{
let sound = document.createElement("audio");
sound.src = src;
sound.setAttribute("preload", "auto");
sound.setAttribute("controls", "none");
sound.style.display = "none";
document.body.appendChild(sound);
this.sounds[key] = sound;
};
playFor = (key,time) => {
if(!this.#state) return;
this.sounds[key].play();
setTimeout(()=>{
this.sounds[key].pause();
this.sounds[key].currentTime = 0;
},time);
};
play = (key) => {
if(!this.#state) return;
this.sounds[key].currentTime = 0;
this.sounds[key].play();
};
playInfinite = (key) =>{
if(!this.#state) return;
this.sounds[key].play();
};
stop = (key) => {
if(!this.#state) return;
this.sounds[key].pause();
};
}