Jump to content

How can I play sounds in sequence without an audio gap using Phaser?


Recommended Posts

I have a project where I have parts of a song broken into separate audio files that I need to play in a random sequence without a gap in the audio. I verified that the sounds are seamless by looping them in both audio editing programs and in Phaser after learning to use m4a files instead of mp3 files. Unfortunately, there is a gap between the audio files that I can't figure out how to get rid of. Here is the code that I am using. Anyone have any ideas about how to solve this problem? Any help would be much appreciated. Thanks!


function songTest() {	var song = game.add.audio(game.rnd.pick(['song1', 'song2', 'song3']));	song.play();	song.onStop.add(songTest, game);}
Link to comment
Share on other sites



Is it a big or a small gap ?


Maybe game.add.audio is taking a little time to create the Sound object.

If you don't have a tremendous amount of different sounds, you can do all the game.add.audio() in the initialization of your game, and just pick a random one at runtime.

Pseudo-code :


in the create function :


this.mySounds = [];





in songTest :


var song = this.mySounds[Math.random() * this.mySounds.length << 0];

song.onStop.addOnce(songTest, game);



On another note, you could also create an audiosprite, si you have only one sound loaded.

Link to comment
Share on other sites

It is a very short gap. I actually thought about that too and previously tried creating the sounds ahead of time and again now just to make sure, but there's still a gap.

I did not know about AudioSprite. Reviewing the docs at http://docs.phaser.io/Phaser.AudioSprite.html, I do not see a method to either detect an individual sound's position or when it completes, so I'm not sure how that would work for me.

Link to comment
Share on other sites

  • 2 weeks later...

Webaudio would allow queueing into future, but Phaser Sound only fires the play when called. Thus is causes small gaps. To have seamless playback you need a bit latency and tell in advance the webaudio to play next sound. This is how real sequencers work anyhow.

Link to comment
Share on other sites


  • Recently Browsing   0 members

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