-
Notifications
You must be signed in to change notification settings - Fork 0
/
soundUtil.js
84 lines (53 loc) · 1.72 KB
/
soundUtil.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
class audioHolder
{
constructor(audioPaths){
this.loaded = 0;
this.totalPaths;
this.preloadedSounds = [];
}
PreloadAudio(url,playbackRt = 1) {
const $audio = new Audio();
// once this file loads, it will call loadedAudio()
// the file will be kept by the browser as cache
const $self = this;
$audio.addEventListener('canplaythrough', function(){$self.LoadedAudio()}, false);
$audio.src = url;
$audio.playbackRate = playbackRt;
this.preloadedSounds.push($audio);
}
LoadedAudio(){
console.log("LOADED");
console.log(this);
this.loaded++;
if (this.loaded == this.preloadedSounds.length){
// all have loaded
this.Init();
}
}
Init(){
let audios = this.preloadedSounds;
for(let i=0; i<audios.length; i++){
if(i != 0){
audios[i-1].addEventListener('ended', function(){
try{
audios[i].play();
}
catch{
console.log("fail");
}
})
}
}
//audios[0].playbackRate = 0.5;
console.log(audios[0].playbackRate);
audios[0].play();
}
}
export function PlaySequentialSounds(soundPaths,debugLogObject = null,playbackRate = 1){
const $audioHolder = new audioHolder();
$audioHolder.totalPaths = soundPaths.length;
// we start preloading all the audio files
for (const snd of soundPaths) {
$audioHolder.PreloadAudio(snd,playbackRate);
}
}