royibernthal Posted May 8, 2014 Share Posted May 8, 2014 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(); //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:http://examples.phaser.io/_site/view_full.html?d=audio&f=audio+sprite.js&t=audio%20sprite 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 More sharing options...
royibernthal Posted May 9, 2014 Author Share Posted May 9, 2014 bump Link to comment Share on other sites More sharing options...
valueerror Posted May 9, 2014 Share Posted May 9, 2014 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() functiongame.sound.play('jump');thats all and when playing "jump" the background sound keeps playing.. Link to comment Share on other sites More sharing options...
royibernthal Posted May 9, 2014 Author Share Posted May 9, 2014 What happens if you try to play a jump sound when a jump sound is already playing? Will you be hearing 2 jump sounds or just one? Link to comment Share on other sites More sharing options...
valueerror Posted May 9, 2014 Share Posted May 9, 2014 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 More sharing options...
royibernthal Posted May 9, 2014 Author Share Posted May 9, 2014 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 More sharing options...
stasuss Posted May 9, 2014 Share Posted May 9, 2014 If you look at the play() method closely, you will notice that the last argument, called forceRestart, is 'true' by default. Link to comment Share on other sites More sharing options...
royibernthal Posted May 9, 2014 Author Share Posted May 9, 2014 Yes I noticed that, I set it to false in the commented line of the first code I posted, mentioning there that it didn't work as well. As for SoundManager.play(), there's no forceRestart argument there. Link to comment Share on other sites More sharing options...
valueerror Posted May 9, 2014 Share Posted May 9, 2014 well.. i converted "worldclear" to mp3 and tried again.. everything works fine.. i can trigger the melody withgame.sound.play('worldclear');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 More sharing options...
stasuss Posted May 9, 2014 Share Posted May 9, 2014 May be you are using the apple device? I've heard that they limiting to only one sound playing at the same time. But may be it is not true anymore. Is WebAudio is available? Link to comment Share on other sites More sharing options...
valueerror Posted May 9, 2014 Share Posted May 9, 2014 also the webbrowser could be responsible.. i'm using chromium 29 on linux Link to comment Share on other sites More sharing options...
royibernthal Posted May 10, 2014 Author Share Posted May 10, 2014 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 More sharing options...
valueerror Posted May 10, 2014 Share Posted May 10, 2014 ahh internet explorer my good old friend You know you can start chrome with additional parameters to allow loadingof local files and lowering the security.. Link to comment Share on other sites More sharing options...
royibernthal Posted May 10, 2014 Author Share Posted May 10, 2014 Did you mean "my old enemy"? I tried adding --allow-file-access-from-files but I still get errors, what parameters are you using? Link to comment Share on other sites More sharing options...
jpdev Posted May 11, 2014 Share Posted May 11, 2014 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 127.0.0.1) 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 More sharing options...
valueerror Posted May 11, 2014 Share Posted May 11, 2014 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 thispython -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 127.0.0.1:80 Link to comment Share on other sites More sharing options...
dr.au Posted February 5, 2016 Share Posted February 5, 2016 @royibernthal if you notice in the Phaser audio example there's one line of code that allows multiple playback without clipping/restarting. I had the same double jump audio issue and fixed it with this: sfxjump = game.add.audio('jump'); sfxjump.allowMultiple = true; Link to comment Share on other sites More sharing options...
ForgeableSum Posted September 8, 2016 Share Posted September 8, 2016 This doesn't work for me in 2.6.1. If you have the same sound playing at different volumes, clipping inevitably occurs. Befive.Info 1 Link to comment Share on other sites More sharing options...
Befive.Info Posted January 6, 2017 Share Posted January 6, 2017 sfxjump.allowMultiple = true; @dr.au Thanks. It helped. Working for me in Phaser v2.6.2 (OSX El capitan, Safari, Chrome, firefox) Link to comment Share on other sites More sharing options...
Sebastien Posted June 4, 2017 Share Posted June 4, 2017 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 : this.load.audiosprite('sounds', [ 'assets/audio/sounds.ogg', 'assets/audio/sounds.m4a', 'assets/audio/sounds.ac3', 'assets/audio/sounds.mp3' ], 'assets/audio/sounds.json' ); 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){ TD.sounds.play('musketSound1'); }else if(rnd < 0.4){ TD.sounds.play('musketSound2'); }else if(rnd < 0.6){ TD.sounds.play('musketSound3'); }else if(rnd < 0.8){ TD.sounds.play('musketSound4'); }else{ TD.sounds.play('musketSound5'); } 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 More sharing options...
Andres Posted June 23, 2017 Share Posted June 23, 2017 @Sebastian 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 More sharing options...
Recommended Posts