-
Notifications
You must be signed in to change notification settings - Fork 4
/
soundbox.js
73 lines (62 loc) · 2.16 KB
/
soundbox.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
/**
* SoundBox
* By Starbeamrainbowlabs
* License: MIT License
* A super simple JS library for playing sound effects and other audio.
*
* Note to self: When making a release, remember to update the version number at the bottom of the file!
*/
"use strict";
class SoundBox {
constructor() {
this.sounds = {}; // The loaded sounds and their instances
this.instances = []; // Sounds that are currently playing
this.default_volume = 1;
}
load(sound_name, path, callback) {
this.sounds[sound_name] = new Audio(path);
if(typeof callback == "function")
this.sounds[sound_name].addEventListener("canplaythrough", callback);
else
return new Promise((resolve, reject) => {
this.sounds[sound_name].addEventListener("canplaythrough", resolve);
this.sounds[sound_name].addEventListener("error", reject);
});
};
remove(sound_name) {
if(typeof this.sounds != "undefined")
delete this.sounds[sound_name];
};
play(sound_name, callback, volume = null, loop = false) {
if(typeof this.sounds[sound_name] == "undefined") {
console.error("Can't find sound called '" + sound_name + "'.");
return false;
}
var soundInstance = this.sounds[sound_name].cloneNode(true);
soundInstance.volume = typeof volume === 'number' ? volume : this.default_volume;
soundInstance.loop = loop;
soundInstance.play();
this.instances.push(soundInstance);
// Don't forget to remove the instance from the instances array
soundInstance.addEventListener("ended", () => {
let index = this.instances.indexOf(soundInstance);
if(index != -1) this.instances.splice(index, 1);
});
// Attach the callback / promise
if(typeof callback == "function") {
soundInstance.addEventListener("ended", callback);
return true;
}
return new Promise((resolve, reject) => soundInstance.addEventListener("ended", resolve));
};
stop_all() {
// Pause all currently playing sounds
// Shallow clone the array to avoid issues with instances auto-removing themselves
let instances_to_stop = this.instances.slice();
for(let instance of instances_to_stop) {
instance.pause();
instance.dispatchEvent(new Event("ended"));
}
}
}
SoundBox.version = "0.3.8";