forked from dogeared/pacman
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathaudio.js
More file actions
83 lines (67 loc) · 2.04 KB
/
audio.js
File metadata and controls
83 lines (67 loc) · 2.04 KB
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
function Audio(game) {
var files = [],
endEvents = [],
progressEvents = [],
playing = [];
function load(name, path, cb) {
var f = files[name] = document.createElement("audio");
progressEvents[name] = function (event) { progress(event, name, cb); };
f.addEventListener("canplaythrough", progressEvents[name], true);
f.setAttribute("preload", "true");
f.setAttribute("autobuffer", "true");
f.setAttribute("src", path);
f.pause();
};
function progress(event, name, callback) {
if (event.loaded === event.total && typeof callback === "function") {
callback();
files[name].removeEventListener("canplaythrough",
progressEvents[name], true);
}
};
function disableSound() {
for (let i = 0; i < playing.length; i++) {
files[playing[i]].pause();
files[playing[i]].currentTime = 0;
}
playing = [];
};
function ended(name) {
var tmp = [], found = false;
files[name].removeEventListener("ended", endEvents[name], true);
for (let i = 0; i < playing.length; i++) {
if (!found && playing[i]) {
found = true;
} else {
tmp.push(playing[i]);
}
}
playing = tmp;
};
function play(name) {
if (!game.soundDisabled()) {
endEvents[name] = function () { ended(name); };
playing.push(name);
files[name].addEventListener("ended", endEvents[name], true);
files[name].play();
}
};
function pause() {
for (let i = 0; i < playing.length; i++) {
files[playing[i]].pause();
}
};
function resume() {
for (let i = 0; i < playing.length; i++) {
files[playing[i]].play();
}
};
return {
disableSound: disableSound,
load: load,
play: play,
pause: pause,
resume: resume
};
};
export { Audio };