MarkPal

Audio not looping in Chrome

Recommended Posts

Hello all,

I've nearly finished one big game using Phaser and today I encountered a problem I haven't noticed before: all the audio files I have set to loop doesn't loop in Chrome browser. This happens with both desktop and mobile. When I play my game using Opera or Firefox all these audio files play flawlessly and loop. I'm using ogg and m4a as file formats for music files. For sfx I'm using wav, ogg and m4a. Have you got any ideas what could be the cause of this problem?

I'm using Phaser 2.1.2 and for looping sounds I'm doing this:

this.levelMusic.play('', 0, 1, true);

In Chrome, despite the boolean value for loop being true, the audio file plays only once. If I check from console the value of looping (this.levelMusic.loop) it also says it's true.

 

All help is much appreciated.

 

- Marko

Share this post


Link to post
Share on other sites

I experienced a simular issues. Though I'm using Phaser 2.2.0.

After taking it out my fadein event the sound looped. No idea why.

 

I use now:

 

        sndGame.loop = true;
        sndGame.play();

        //sndGame.fadeIn(2000);

Share this post


Link to post
Share on other sites
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create });function preload() {    game.load.image('speakers','assets/sprites/speakers.png');    game.load.audio('bass', 'assets/audio/tech/bass.mp3');    game.load.audio('drums', 'assets/audio/tech/drums.mp3');    game.load.audio('percussion', 'assets/audio/tech/percussion.mp3');    game.load.audio('synth1', 'assets/audio/tech/synth1.mp3');    game.load.audio('synth2', 'assets/audio/tech/synth2.mp3');    game.load.audio('top1', 'assets/audio/tech/top1.mp3');    game.load.audio('top2', 'assets/audio/tech/top2.mp3');}var bass;var drums;var percussion;var synth1;var synth2;var top1;var top2;var text;var sounds;var current;var speakers;var loopCount = 0;function create() {    game.stage.backgroundColor = '#838282';    speakers = game.add.image(game.world.centerX, 500, 'speakers');    speakers.anchor.set(0.5, 1);    var style = { font: "65px Arial", fill: "#52bace", align: "center" };    text = game.add.text(game.world.centerX, 100, "decoding", style);    text.anchor.set(0.5);    bass = game.add.audio('bass');    drums = game.add.audio('drums');    percussion = game.add.audio('percussion');    synth1 = game.add.audio('synth1');    synth2 = game.add.audio('synth2');    top1 = game.add.audio('top1');    top2 = game.add.audio('top2');        console.log("bass!" + bass);                bass.onDecoded.addOnce(start, this);    sounds = [ bass, drums, percussion, synth1, synth2, top1, top2 ];    //  Being mp3 files these take time to decode, so we can't play them instantly    //  Using setDecodedCallback we can be notified when they're ALL ready for use.    //  The audio files could decode in ANY order, we can never be sure which it'll be.    //game.sound.setDecodedCallback(sounds, start, this);        start();}function start() {        bass.play("",0,1,true,true);    bass.onLoop.add(hasLooped, this);    text.text = 'bass';}function hasLooped(sound) {    console.log("bass looped!");}

This example (played through the sandbox) works in 2.3.0 but fails in previous version (2.2.2 and earlier). This is in Chrome only.

Share this post


Link to post
Share on other sites

Yes there was a web audio looping specific bug fixed in 2.3 (where the loop property was set, but ignored because it still specified an offset position, Chrome changed and this no longer works for looping, so now you just set loop to true and DON'T give any offset like we do in 2.3 and all is fine again). From the 2.3 change log:

Sound in Web Audio now uses AudioContext.onended to trigger when it will stop playing instead of using a time based value. This is only used if the sound doesn't loop and isn't an audio sprite, but will give a much more accurate `Sound.onStop` event. It also prevents short audio files from being cut off during playback (#1471) and accounts for time spent decoding.

and

Sound.loop even when set for WebAudio wouldn't use the AudioContext loop property because Sound.start was being invoked with an offset and duration. Now if `loop` is true and no marker is being used it will use the native Web Audio loop support (#1431)

Share this post


Link to post
Share on other sites

 

Yes there was a web audio looping specific bug fixed in 2.3 (where the loop property was set, but ignored because it still specified an offset position, Chrome changed and this no longer works for looping, so now you just set loop to true and DON'T give any offset like we do in 2.3 and all is fine again).

 

Sorry for being thick but if you want to play a looping sound AND have a starting position other than 0, is that possible? Is it possible only in 2.3?

Share this post


Link to post
Share on other sites

Is not clear for me if this is solved for MarkPal...but what I'm using now is the following code to make a loop (Working in chrome)

//Start the soundtrack!soundtrack = game.add.audio('bso');soundtrack.loop = true;soundtrack.play();

Share this post


Link to post
Share on other sites

Thank you all for your tips and clarifications. I got it to work by doing this:
 

this.levelMusic.play('', 0, 1, true);this.levelMusic.onLoop.add(this.playLevelMusic, this);

And then with the function playLevelMusic I set it to play once again:
 

playLevelMusic: function() {	this.levelMusic.play('', 0, 1, true);}

That way it seems to work for me. Somewhat similar to phantomas' solution. Dunno if this is very efficient, though.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.