-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassetmanager.js
66 lines (53 loc) · 1.68 KB
/
assetmanager.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
class AssetManager {
constructor() {
this.successCount = 0;
this.errorCount = 0;
this.musicCache = {};
this.musicDownloadQueue = [];
};
queueDownloadMusic(path) {
this.musicDownloadQueue.push(path);
}
isDone() {
return this.musicDownloadQueue.length === this.successCount + this.errorCount;
};
#downloadAllMusic(callback) {
if (this.musicDownloadQueue.length === 0) setTimeout(callback, 10);
for (let i = 0; i < this.musicDownloadQueue.length; i++) {
const audio = new Audio();
const path = this.musicDownloadQueue[i];
audio.addEventListener("loadeddata", () => {
this.successCount++;
console.log(`${path} loaded`)
if (this.isDone()) callback();
});
audio.addEventListener("error", () => {
this.errorCount++;
if (this.isDone()) callback();
});
audio.addEventListener("ended", () => {
audio.pause()
audio.currentTime = 0
});
audio.src = path;
audio.volume = 0.5
audio.load()
this.musicCache[path] = audio;
}
};
downloadAll(callback) {
this.#downloadAllMusic(callback)
};
getMusicByPath(path) {
return this.musicCache[path];
};
playMusic(path) {
const musicPtr = this.getMusicByPath(path)
if (document.getElementById("mute").checked) {
musicPtr.volume = 0
} else {
musicPtr.volume = document.getElementById("volume").value
}
musicPtr.play()
}
}