From 7f741a509cd883146344b22a4a002c5ecba3fd7b Mon Sep 17 00:00:00 2001 From: Eric Rosenbaum Date: Mon, 30 Jan 2017 18:12:52 -0500 Subject: [PATCH] sounds stop and restart when played again --- src/SoundPlayer.js | 1 + src/index.js | 13 ++++++++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/SoundPlayer.js b/src/SoundPlayer.js index b353966..ae4722b 100644 --- a/src/SoundPlayer.js +++ b/src/SoundPlayer.js @@ -6,6 +6,7 @@ function SoundPlayer () { this.buffer; // a Tone.Buffer this.bufferSource; this.playbackRate = 1; + this.isPlaying = false; } SoundPlayer.prototype.connect = function (node) { this.outputNode = node; diff --git a/src/index.js b/src/index.js index f1c4f94..eca7310 100644 --- a/src/index.js +++ b/src/index.js @@ -144,7 +144,6 @@ function AudioPlayer (audioEngine) { AudioPlayer.prototype.playSound = function (md5) { // if this sprite or clone is already playing this sound, stop it first - // (this is not working, not sure why) if (this.activeSoundPlayers[md5]) { this.activeSoundPlayers[md5].stop(); } @@ -159,10 +158,14 @@ AudioPlayer.prototype.playSound = function (md5) { // add it to the list of active sound players this.activeSoundPlayers[md5] = player; - // when the sound completes, remove it from the list of active sound players - return player.finished().then(() => { - delete this.activeSoundPlayers[md5]; - }); + // remove sounds that are not playing from the active sound players array + for (var id in this.activeSoundPlayers) { + if (!this.activeSoundPlayers[id].isPlaying) { + delete this.activeSoundPlayers[id]; + } + } + + return player.finished(); }; AudioPlayer.prototype.playDrumForBeats = function (drum, beats) {