Skip to content

Commit

Permalink
Fixes memory leak on webaudio resolve #64
Browse files Browse the repository at this point in the history
  • Loading branch information
Trpzn committed Mar 22, 2017
1 parent 3753066 commit 5735dbf
Showing 1 changed file with 25 additions and 14 deletions.
39 changes: 25 additions & 14 deletions src/sound/Sound.js
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*/
Expand All @@ -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);
}
}


},

/**
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down

0 comments on commit 5735dbf

Please sign in to comment.