Skip to content

Commit

Permalink
Bugfix: shallow clone instances array now that we remove instances vi…
Browse files Browse the repository at this point in the history
…a the ended event to ensure we hit em all
  • Loading branch information
sbrl committed May 21, 2018
1 parent 10bd465 commit 12a988a
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 18 deletions.
1 change: 1 addition & 0 deletions example.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ <h1>SoundBox.js Example</h1>
<button id="play_multiple_sounds_promise">Play Multiple Sounds with Promises</button>

<button id="play_quiet">Play Quiet Sound</button>
<button id="play_lots">Play Lots of Sounds (1s delay)</button>
</p>

<p>
Expand Down
21 changes: 15 additions & 6 deletions example.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -42,14 +42,23 @@ window.addEventListener("load", function(event) {
});
});
});

document.getElementById("play_multiple_sounds_promise")
.addEventListener("click", function() {
window.soundbox.play("beep1")
.then(() => window.soundbox.play("beep2"))
.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();
Expand Down
11 changes: 7 additions & 4 deletions soundbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand All @@ -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"));
}
}
}

Expand Down
11 changes: 7 additions & 4 deletions soundbox.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand All @@ -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"));
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions soundbox.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions soundbox.min.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 12a988a

Please sign in to comment.