BrunoHautenfaust Posted November 11, 2015 Share Posted November 11, 2015 I want to loop an audio from a specific point. I'm using markers. I can specify starting point but the audio is not looping.Also, how do I specify in the marker that I want it to play till the end of the file? intro.duration, intro.length aren't the answers. var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update, render: render});var intro, marker;function preload() { game.load.audio('intro', 'intro.ogg');}function create() { intro = game.add.audio('intro'); marker = intro.addMarker('marker', 2.3, 8.26, true); // true, but not looping and 8.26 must be something like intro.duration or intro.<rest of the file> space = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);}function update() { if (space.isDown) { intro.play('marker', true); // // true, but not looping }}OK. I found some sort of a solution:var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update});var intro, marker;function preload() { game.load.audio('intro_in', 'intro_in.ogg'); game.load.audio('intro_rest', 'intro_rest.ogg');}function create() { intro_in = game.add.audio('intro_in'); intro_rest = game.add.audio('intro_rest', 1, true); space = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);}function update() { if (space.isDown) { intro_in.play(); game.time.events.add(200, function() { intro_rest.play();}, this); }} The idea: The audio is divided in 2 parts. First part 1 plays. And after some delay the second plays with a loop.! And something important to point out: var song = game.add.audio('name', true); DOES NOT loop the song.BUTvar song = game.add.audio('name', 1, true); DOES Is this a bug? I took a peek in the source code here but everything seems fine.==== I'll mark this as answered tomorrow but I'll wait to see if anyone can propose a more elegant solution. Something with less code maybe. Link to comment Share on other sites More sharing options...
YuShaN Posted November 12, 2015 Share Posted November 12, 2015 How about use .loop() .stop() .play() ? Link to comment Share on other sites More sharing options...
Skeptron Posted November 12, 2015 Share Posted November 12, 2015 Shouldn't you use the soundManager instead of directly calling play() on the sound itself? Like this.game.sound.play("mySound", 1, true)? Doc : http://phaser.io/docs/2.3.0/Phaser.SoundManager.html#play var song = game.add.audio('name', true); DOES NOT loop the song.BUTvar song = game.add.audio('name', 1, true); DOES Yeah it's normal, if you look at the API : http://phaser.io/docs/2.4.4/Phaser.GameObjectFactory.html#audio It's audio(key, volume, loop, connect). You cannot omit the volume parameter if you want to use the "loop" parameter. Link to comment Share on other sites More sharing options...
BrunoHautenfaust Posted November 12, 2015 Author Share Posted November 12, 2015 How about use .loop() .stop() .play() ? Didn't see any loop() method. But there's a loopFull(volume) which should work in theory. I'll give it a... "loop". Shouldn't you use the soundManager instead of directly calling play() on the sound itself? Like this.game.sound.play("mySound", 1, true)? Doc : http://phaser.io/docs/2.3.0/Phaser.SoundManager.html#play Yeah it's normal, if you look at the API : http://phaser.io/docs/2.4.4/Phaser.GameObjectFactory.html#audio It's audio(key, volume, loop, connect). You cannot omit the volume parameter if you want to use the "loop" parameter. The SoundManager is an idea.As for the audio -> volume, loop and connect are optional parameters with some default values. I don't see why should I explicitly write all, if I want to use only one of them. Link to comment Share on other sites More sharing options...
Skeptron Posted November 12, 2015 Share Posted November 12, 2015 Because it's the way Javascript works (not Phaser, javascript). You can omit parameters but if you want to use some, you have to use all previous parameters up to this one. So if you want to use "loop", you MUST precise "key", "volume" and finally "loop". BrunoHautenfaust 1 Link to comment Share on other sites More sharing options...
BrunoHautenfaust Posted November 12, 2015 Author Share Posted November 12, 2015 Because it's the way Javascript works (not Phaser, javascript). You can omit parameters but if you want to use some, you have to use all previous parameters up to this one. So if you want to use "loop", you MUST precise "key", "volume" and finally "loop". Now there's a tiny significant detail about JS I didn't know. Cool! loopFull() works fine. Can be used to loop as well.Playing around with the SoundManager and Sounds pointed me towards an idea. I think I'm close since I've figured out how to works this out using Phaser Signals(something that I found confusing until know but began to understand). function create() { intro = game.add.audio('intro'); marker = intro.addMarker('marker', 2.5, 5.0, 1, true); // If I remove this, the audio plays and it reaches the callback 'nowPlayThis' space = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR); intro.onStop.add(nowPlayThis, this); // I read in the docs that the listener 'this' is optional}function update() { if (space.isDown) { intro.play(); }}function nowPlayThis() { intro.play('marker');}I think the idea is clear. When the audio plays once, it fires up the nowPlayThis callback which plays the marker in a loop. I only have to figure out why this won't play at all, though. Link to comment Share on other sites More sharing options...
Skeptron Posted November 12, 2015 Share Posted November 12, 2015 I'm not an expert with sounds, but maybe you could use game.make.audio('intro') instead of game.add.audio('intro'). That way, you can first create all your sounds and set up their callbacks/signals, THEN later on play them (don't forget to invoke play() on them, as it wouldn't be done automatically). From what I understand, game.add adds the sound to the world and plays it, which is not really the best scenario here : first set up everything, then play once it's done. That way the playing of the sound shouldn't be interrupted. GameObjectFactory (game.add) doc : http://phaser.io/docs/2.4.4/Phaser.GameObjectFactory.htmlGameObjectCreator (game.make) doc : http://phaser.io/docs/2.4.4/Phaser.GameObjectCreator.html Link to comment Share on other sites More sharing options...
BrunoHautenfaust Posted November 12, 2015 Author Share Posted November 12, 2015 intro = game.add.audio('intro'); is adding the audio, but it's not playing it unless I tell it to. And I find 'add' good enough a method because "[game.add] Created objects are automatically added to the appropriate Manager, World, or manually specified parent Group." I've never used nor seen anywhere game.make.<something>. That way, you can first create all your sounds and set up their callbacks/signals, THEN later on play them (don't forget to invoke play() on them, as it wouldn't be done automatically). I didn't understand. How do I link an object from 'create' to 'play'? function create() {intro = game.make.audio('intro') // object with the 'intro' audio is created.// And then: intro = game.add.audio('intro');// But that's pointless because the later overwrites the first.}function play() { intro.play(); // Will constantly start from the first second or ms but that's not important here} Link to comment Share on other sites More sharing options...
Skeptron Posted November 12, 2015 Share Posted November 12, 2015 Ok, don't mind my last comment, I didn't even see that you play the sound if the space bar is down. I thought you wanted it to play right after being created. So I don't know! If I understand your issue correctly : adding a marker breaks the sound? Maybe if you do so Phaser considers you need to play the markers and not the original sound anymore. The doc is broken, access to code doesn't work, so I can't know what the method does, really. :'( Link to comment Share on other sites More sharing options...
BrunoHautenfaust Posted November 12, 2015 Author Share Posted November 12, 2015 I was testing with space to have better control. So I don't know! If I understand your issue correctly : adding a marker breaks the sound? Maybe if you do so Phaser considers you need to play the markers and not the original sound anymore. That seems to be the case.I experimented with the Audio examples and it turned out that this callback idea would work only if I play a marker and then start another marker. Which for some reason loops endlessly. Don't know why. Can't set it to false. MARKER EXAMPLE:var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create });function preload() { game.load.audio('sfx', 'assets/audio/SoundEffects/fx_mixdown.ogg');}var fx;function create() { fx = game.add.audio('sfx'); fx.addMarker('meow', 8, 0.5); fx.addMarker('boss hit', 3, 0.5); fx.play('meow'); fx.onStop.add(hasLooped, this);}function hasLooped() { fx.play('boss hit');}Paste this code -> here-----------... Or not use markers at all.AUDIO EXAMPLE(no markers):var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create });function preload() { game.load.audio('top1', 'assets/audio/tech/top1.mp3'); game.load.audio('top2', 'assets/audio/tech/top2.mp3');}var top1;var top2;function create() { top1 = game.add.audio('top1'); top2 = game.add.audio('top2'); top1.onStop.add(nowPlayThis, this);}function nowPlayThis() { top2.play();}Paste this code -> here Bottom line: markers are if you have an audio file with many sounds in it and you want to select some/all of them at different times. P.S. I would've posted a sanbox link but the assets' paths, provided in the examples, can't be accessed in the sandbox.======= And Skeptron's comment which is important: Ok I found the doc : https://github.com/photonstorm/phaser/blob/master/src/sound/Sound.js#L543 If you don't pass a marker when playing the sound, the marker is automatically assigned to '' (see https://github.com/photonstorm/phaser/blob/master/src/sound/Sound.js#L501), and if you try to play a full sound that has markers without defining which marker, it won't play. So yeah, once you put markers in your sound, you shall ONLY use markers. Link to comment Share on other sites More sharing options...
Skeptron Posted November 12, 2015 Share Posted November 12, 2015 Ok I found the doc : https://github.com/photonstorm/phaser/blob/master/src/sound/Sound.js#L543 If you don't pass a marker when playing the sound, the marker is automatically assigned to '' (see https://github.com/photonstorm/phaser/blob/master/src/sound/Sound.js#L501), and if you try to play a full sound that has markers without defining which marker, it won't play. So yeah, once you put markers in your sound, you shall ONLY use markers. BrunoHautenfaust 1 Link to comment Share on other sites More sharing options...
BrunoHautenfaust Posted November 12, 2015 Author Share Posted November 12, 2015 And another one bites the dust. Link to comment Share on other sites More sharing options...
Recommended Posts