Jump to content

Problem with listeners using Phaser.Sound.onPlay.add()


Pappa
 Share

Recommended Posts

Hi,

 

I'm new to Phaser and I'm seeing an issue that looks like a memory leak, but I'm not sure if I'm using the onPlay Signal correctly.

 

I'm using Phaser v2.4.3, and here's a simplified version of the code I'm working with:

var PhaserGarbageTest = PhaserGarbageTest || {};PhaserGarbageTest.GameState = {  init: function() {	this.cursors = this.game.input.keyboard.createCursorKeys();  },  preload: function() {    this.load.audio('splat', ['asset/audio/splat.mp3', 'asset/audio/splat.ogg']);  },  create: function() {    this.splatSound = this.add.audio('splat');	this.splatSound.onPlay.add(this.onSoundPlay, this);	this.splatSound.onStop.add(this.onSoundStop, this);  },  update: function() {	  if ((this.cursors.up.isDown || this.game.input.activePointer.isDown) && !this.splatSound.isPlaying) {	    this.splatSound.play();	  }  },  onSoundPlay: function (sound) {    console.log("onSoundPlay");  },  onSoundStop: function (sound) {    console.log("onSoundStop");  }};

And here's the Chrome dev tools timeline recorded while I'm repeatedly playing the sound at short intervals, with a few small pauses:

 

post-16199-0-34159600-1441213456.png

 

It seems that the listeners are being added to the document each time the sound is played. Can anyone shed some light on what I'm doing wrong?

Link to comment
Share on other sites

Here's another Chrome timeline. This time over a longer period (140 seconds), and with about 250 onPlay callbacks being fired. Garbage collection is occurring as expected, giving me the standard sawtooth, but the listener count continues to rise until the Chrome timeline buffer is full.

 

post-16199-0-18867400-1441301149.png

Link to comment
Share on other sites

This seems to be caused by a memory leak in Phaser.Sound. A reference to the AudioBufferSourceNode is kept hanging around. I have been able to fix it by modifying Phaser.Sound.prototype.onEndedHandler:

    onEndedHandler: function () {                this._sound.onended = null;        this.isPlaying = false;        this.stop();    },
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...