diff --git a/src/sound/Sound.js b/src/sound/Sound.js index 464c3c5a8..a1c59a446 100644 --- a/src/sound/Sound.js +++ b/src/sound/Sound.js @@ -175,6 +175,12 @@ Phaser.Sound = function (game, key, volume, loop, connect) { */ this._sound = null; + /** + * @property {object} _markedToDelete - When audio stops if markedToDelete it's active, will disconnect the nodes and free the memory. + * @private + */ + this._markedToDelete = false; + if (this.usingWebAudio) { this.context = this.game.sound.context; @@ -388,7 +394,7 @@ Phaser.Sound.prototype = { /** * Called automatically by the AudioContext when the sound stops playing. * Doesn't get called if the sound is set to loop or is a section of an Audio Sprite. - * + * * @method Phaser.Sound#onEndedHandler * @protected */ @@ -399,6 +405,18 @@ Phaser.Sound.prototype = { this.currentTime = this.durationMS; this.stop(); + if(this._markedToDelete){ + if (this.externalNode) + { + this._sound.disconnect(this.externalNode); + } + else if (this.gainNode) + { + this._sound.disconnect(this.gainNode); + } + } + + }, /** @@ -504,7 +522,7 @@ Phaser.Sound.prototype = { /** * Play this sound, or a marked section of it. - * + * * @method Phaser.Sound#play * @param {string} [marker=''] - If you want to play a marker then give the key here, otherwise leave blank to play the full sound. * @param {number} [position=0] - The starting position to play the sound from - this is ignored if you provide a marker. @@ -561,7 +579,7 @@ Phaser.Sound.prototype = { if (marker === '' && Object.keys(this.markers).length > 0) { - // If they didn't specify a marker but this is an audio sprite, + // If they didn't specify a marker but this is an audio sprite, // we should never play the entire thing return this; } @@ -873,22 +891,15 @@ Phaser.Sound.prototype = { else { try { + this._markedToDelete = true; this._sound.stop(0); + } catch (e) { // Thanks Android 4.4 } } - - if (this.externalNode) - { - this._sound.disconnect(this.externalNode); - } - else if (this.gainNode) - { - this._sound.disconnect(this.gainNode); - } } else if (this.usingAudioTag) { @@ -948,7 +959,7 @@ Phaser.Sound.prototype = { this.fadeTo(duration, 1); }, - + /** * Decreases the volume of this Sound from its current value to 0 over the duration specified. * At the end of the fade Sound.onFadeComplete is dispatched with this Sound object as the first parameter, @@ -965,7 +976,7 @@ Phaser.Sound.prototype = { /** * Fades the volume of this Sound from its current value to the given volume over the duration specified. - * At the end of the fade Sound.onFadeComplete is dispatched with this Sound object as the first parameter, + * At the end of the fade Sound.onFadeComplete is dispatched with this Sound object as the first parameter, * and the final volume (volume) as the second parameter. * * @method Phaser.Sound#fadeTo