Jump to content

Problem with playing sounds at the same time


Recommended Posts

I have a sound, each time I'm calling sound.play() it restarts the same sound, meaning if the sound is already playing - it is stopped then played from the beginning.


What I want is to create a new sound instance that will play simultaneously with other sounds and never stop previously played sounds.



var sounds = new Object();

function playSound(key)
if (sounds[key] == null) sounds[key] = game.add.audio(key, 1);

var sound = sounds[key];

//sound.play("", 0, 1, false); //doesn't make a difference

return sound;


for instance if on click I call playSound("click") it'll always have only 1 instance running at a time. (the instances thing is a guess, I'm not really sure how it actually works)


Calling game.add.audio() everytime instead of reusing the sound doesn't make a difference either.


It looks like this example doesn't have that problem:


btw - I'm aware that I rephrased the same simple problem in many different ways, I just wanted to make sure anybody reading it will understand.

Link to comment
Share on other sites

don't know if i understood your problem but this is how i manage my sounds.. maybe it helps



this is to preload the sounds..

game.load.audio('jump', 'assets/jump.ogg');game.load.audio('theme', 'assets/mariotheme.ogg');


this is to play the background music in a loop  (in the create() function

 music = game.add.audio('theme',1,true);  // key, volume, loop music.play('',0,1,true);

and this is called every time i want to play any additional sound (in the update() function


thats all and when playing "jump" the background sound keeps playing..  

Link to comment
Share on other sites

the jump sound is a bad example because it's to short to say but when i set the "worldclear" sound for example to play when i push space i can trigger the sound whenever i want and it will play simultaneously ..  sounds really bad but i plays several times at once

Link to comment
Share on other sites

I tried using game.sound.play(), the problem persists.



My problem is, if I do what you did - for instance play "worldclear" several times - they won't play simultaneously, every time a new "worldclear" sound is started, the previous one is stopped.


The only difference I noticed in our codes is that you use .ogg, so is it possible that the problem is with .mp3?

Link to comment
Share on other sites

well.. i converted "worldclear" to mp3 and tried again..   everything works fine..  i can trigger the melody with


as often as i want and it will play simultaneously..   so it's probably not the file format


just for your information.. i didn't set up ANY other sound options.. i just tried this with a basic phaser example without a music playing or anything else.. 


i'm using phaser 2.0.5 dev version.. maybe that's it ?

Link to comment
Share on other sites

It was the browser, I used Internet Explorer to test my code locally (had a security issue with loading files on chrome). When I uploaded my files and tested on chrome it was just like you said.


Also it appears Apple no longer has that limit, it worked just as well on Iphone 4's Safari.


Thanks for the help!

Link to comment
Share on other sites

You can test the phaser games locally by using a local webserver.

Webstorm comes with one included, or if you use any other editor you could download XAMPP and set the htdocs directory to your game directory.

With both you can point your browser to localhost (or and your browser gets served websites.


This way it's more like the real thing and you avoid problems like the local loading policy.

Link to comment
Share on other sites

 IE is definitely not my friend ^^


as for the webserver:  i use apache .. on linux this is a matter of "sudo apt-get install apache2"  and it's done...



With python 2.4 and later you can use the SimpleHTTPServer module like this
python -m SimpleHTTPServer [port]
This will start a HTTP server on port 8000 (if no port is specified) serving the files and directories which are in the current working dir.
for Python 3 use
python -m http.server [port]
If you need the web server to also parse php files you should use the php webserver instead of the python webserver:
php -S 
Link to comment
Share on other sites

  • 1 year later...
  • 7 months later...
  • 3 months later...
  • 4 months later...

My game is a Tower Defense, and I need to have several sounds played simultaneously

But it does not work for me either, in my preload file I have this code :


In my game file I have this code in the create function (TD is my game object)

TD.sounds = this.add.audioSprite('sounds');  
TD.sounds.allowMultiple = true;

and somewhere in my code in a function called by the update function I generate an random number to play a different musket sound to get a bit of variety :

if(rnd < 0.2){
    }else if(rnd < 0.4){
    }else if(rnd < 0.6){
    }else if(rnd < 0.8){

My game is a Tower Defense so this code is called by every tower which contains musketeers at the moment they shot.

What happen is, if I have several musketeer (let's say 5) and if 3 of them got a random number between 0 and 0.2, then I will have the 1st musketeer who will shot and the musketSound1 will play, but if the 2nd musketeer shot a little bit later (lets say 0.2 second later and the musketSound1 has a duration of 0.4s), it will restart the musketSound1 instead of creating a new instance of it.

So what can I do to avoid such a behaviour ?


Link to comment
Share on other sites

  • 3 weeks later...


Maybe there is a less ugly way, but for now I'm doing this hack:

this.audio = this.game.add.audioSprite('audios')
this.audio.play = function (marker, volume) {
    if (volume === undefined) { volume = 1; }
    this.sounds[marker].allowMultiple = true
    return this.sounds[marker].play(marker, null, volume, false, false)

The last `false` disables `forceRestart`: https://photonstorm.github.io/phaser-ce/Phaser.Sound.html#play

So I'm replacing the audioSprite.play function because it seems it forces audio restart: https://github.com/photonstorm/phaser-ce/blob/29bc2b2d76f432a34fdb36ecc8ee634e0d8fdaf0/src/sound/AudioSprite.js#L89

Yeah, `allowMultiple` could be set only once, outside of the function, but... It's just an ugly hack until a proper way shows up.

If there is no proper way, maybe this needs a pull request?

Link to comment
Share on other sites


  • Recently Browsing   0 members

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