From 12a988a9372f95d18a1e2057a116b13aa12ff007 Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs SoundBox.js Example
+
diff --git a/example.js b/example.js index 73e7e57..1dc716e 100644 --- a/example.js +++ b/example.js @@ -13,27 +13,27 @@ window.addEventListener("load", function(event) { .addEventListener("click", function() { soundbox.play("beep1"); }); - + document.getElementById("play_sound_2") .addEventListener("click", function() { soundbox.play("beep2"); }); - + document.getElementById("play_sound_3") .addEventListener("click", function() { soundbox.play("beep3"); }); - + document.getElementById("play_long_sound") .addEventListener("click", function() { soundbox.play("long_beep"); }); - + document.getElementById("play_quiet") .addEventListener("click", function() { soundbox.play("beep2", null, 0.25); }); - + document.getElementById("play_multiple_sounds") .addEventListener("click", function() { soundbox.play("beep1", function() { @@ -42,7 +42,7 @@ window.addEventListener("load", function(event) { }); }); }); - + document.getElementById("play_multiple_sounds_promise") .addEventListener("click", function() { window.soundbox.play("beep1") @@ -50,6 +50,15 @@ window.addEventListener("load", function(event) { .then(() => window.soundbox.play("beep3")); }); + document.getElementById("play_lots") + .addEventListener("click", function() { + setTimeout(() => { + for(let i = 0; i < 10; i++) { + window.soundbox.play("long_beep"); + } + }, 1000); + }); + document.getElementById("stop_sounds") .addEventListener("click", function() { window.soundbox.stop_all(); diff --git a/soundbox.js b/soundbox.js index 7d8965a..c005917 100644 --- a/soundbox.js +++ b/soundbox.js @@ -45,7 +45,7 @@ class SoundBox { // Don't forget to remove the instance from the instances array soundInstance.addEventListener("ended", () => { - let index = this.instances.indexOf(soundInstance, 1); + let index = this.instances.indexOf(soundInstance); if(index != -1) this.instances.splice(index, 1); }); @@ -59,10 +59,13 @@ class SoundBox { stop_all() { // Pause all currently playing sounds - for (let instance of this.instances) - instance.dispatchEvent((new Event('ended'))); + + // 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(); - this.instances = []; // Empty the instances array + instance.dispatchEvent(new Event("ended")); + } } } diff --git a/soundbox.jsm b/soundbox.jsm index a588f60..cba8b9a 100644 --- a/soundbox.jsm +++ b/soundbox.jsm @@ -45,7 +45,7 @@ class SoundBox { // Don't forget to remove the instance from the instances array soundInstance.addEventListener("ended", () => { - let index = this.instances.indexOf(soundInstance, 1); + let index = this.instances.indexOf(soundInstance); if(index != -1) this.instances.splice(index, 1); }); @@ -59,10 +59,13 @@ class SoundBox { stop_all() { // Pause all currently playing sounds - for (let instance of this.instances) - instance.dispatchEvent((new Event('ended'))); + + // 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(); - this.instances = []; // Empty the instances array + instance.dispatchEvent(new Event("ended")); + } } } diff --git a/soundbox.min.js b/soundbox.min.js index 3686d65..06af447 100644 --- a/soundbox.min.js +++ b/soundbox.min.js @@ -4,5 +4,5 @@ * 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,c,d){this.sounds[a]=new Audio(c);if("function"==typeof d)this.sounds[a].addEventListener("canplaythrough",d);else return new Promise((b,e)=>{this.sounds[a].addEventListener("canplaythrough",b);this.sounds[a].addEventListener("error",e)})}remove(a){"undefined"!=typeof this.sounds&&delete this.sounds[a]}play(a,c,d=null){if("undefined"==typeof this.sounds[a])return console.error("Can't find sound called '"+ -a+"'."),!1;var b=this.sounds[a].cloneNode(!0);b.volume=d||this.default_volume;b.play();this.instances.push(b);b.addEventListener("ended",()=>{let a=this.instances.indexOf(b,1);-1!=a&&this.instances.splice(a,1)});return"function"==typeof c?(b.addEventListener("ended",c),!0):new Promise((a,c)=>b.addEventListener("ended",a))}stop_all(){for(let a of this.instances)a.dispatchEvent(new Event("ended"));instance.pause();this.instances=[]}}SoundBox.version="0.3.3"; +'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((c,b)=>{this.sounds[a].addEventListener("canplaythrough",c);this.sounds[a].addEventListener("error",b)})}remove(a){"undefined"!=typeof this.sounds&&delete this.sounds[a]}play(a,b,d=null){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=d||this.default_volume;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.3"; diff --git a/soundbox.min.jsm b/soundbox.min.jsm index a100b50..6448197 100644 --- a/soundbox.min.jsm +++ b/soundbox.min.jsm @@ -10,6 +10,6 @@ * 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,c,d){this.sounds[a]=new Audio(c);if("function"==typeof d)this.sounds[a].addEventListener("canplaythrough",d);else return new Promise((b,e)=>{this.sounds[a].addEventListener("canplaythrough",b);this.sounds[a].addEventListener("error",e)})}remove(a){"undefined"!=typeof this.sounds&&delete this.sounds[a]}play(a,c,d=null){if("undefined"==typeof this.sounds[a])return console.error("Can't find sound called '"+ -a+"'."),!1;var b=this.sounds[a].cloneNode(!0);b.volume=d||this.default_volume;b.play();this.instances.push(b);b.addEventListener("ended",()=>{let a=this.instances.indexOf(b,1);-1!=a&&this.instances.splice(a,1)});return"function"==typeof c?(b.addEventListener("ended",c),!0):new Promise((a,c)=>b.addEventListener("ended",a))}stop_all(){for(let a of this.instances)a.dispatchEvent(new Event("ended"));instance.pause();this.instances=[]}}SoundBox.version="0.3.3"; +'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((c,b)=>{this.sounds[a].addEventListener("canplaythrough",c);this.sounds[a].addEventListener("error",b)})}remove(a){"undefined"!=typeof this.sounds&&delete this.sounds[a]}play(a,b,d=null){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=d||this.default_volume;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.3"; export default SoundBox;