-
Notifications
You must be signed in to change notification settings - Fork 4
/
soundbox.min.mjs
15 lines (15 loc) · 1.27 KB
/
soundbox.min.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
* SoundBox
* By Starbeamrainbowlabs
* License: MIT License
* A super simple JS library for playing sound effects and other audio.
*/
/**
* SoundBox
* By Starbeamrainbowlabs
* License: MIT License
* A super simple JS library for playing sound effects and other audio.
*/
'use strict';class SoundBox{constructor(){this.sounds={};this.instances=[];this.default_volume=1}load(a,b,d){this.sounds[a]=new Audio(b);if("function"==typeof d)this.sounds[a].addEventListener("canplaythrough",d);else return new Promise((b,c)=>{this.sounds[a].addEventListener("canplaythrough",b);this.sounds[a].addEventListener("error",c)})}remove(a){"undefined"!=typeof this.sounds&&delete this.sounds[a]}play(a,b,d=null,e=!1){if("undefined"==typeof this.sounds[a])return console.error("Can't find sound called '"+
a+"'."),!1;var c=this.sounds[a].cloneNode(!0);c.volume="number"===typeof d?d:this.default_volume;c.loop=e;c.play();this.instances.push(c);c.addEventListener("ended",()=>{let a=this.instances.indexOf(c);-1!=a&&this.instances.splice(a,1)});return"function"==typeof b?(c.addEventListener("ended",b),!0):new Promise((a,b)=>c.addEventListener("ended",a))}stop_all(){let a=this.instances.slice();for(let b of a)b.pause(),b.dispatchEvent(new Event("ended"))}}SoundBox.version="0.3.8";
export default SoundBox;