Playing .mp3 sound issue in html5 (Chrome)



  • Hi!

    I've used FlxSound to play sound effects in my game. In html5 target and Chrome browser some .mp3 files work correctly, but the others are not played at all.

    In the following example (html5 and chrome) I hear bckMusicPlay but there is no explosionSound.

    What could be wrong here?

    import flixel.FlxState;
    import flixel.system.FlxSound;
    
    class SoundTest extends FlxState
    {
    	public var bckMusicPlay:FlxSound;
    	public var explosionSound:FlxSound;
    	
    	override public function create() 
    	{
    		explosionSound = new FlxSound();
    		explosionSound.loadEmbedded('assets/sounds/explosion.mp3', true, true);
    		explosionSound.play();
    		bckMusicPlay = new FlxSound();
    		bckMusicPlay.loadEmbedded('assets/music/background_play.mp3', true, true);
    		bckMusicPlay.play();
    	}
    	
    }
    


  • just a guess, but maybe the looped=true for explosion sound should be false? Do you really want to loop it? Not sure that would cause your problem, or not.

    You can use FlxG.sound.play() and FlxG.sound.playMusic() to make things easier.

    // if you just want to play it once, you don't need to define the var explosionSound.
    FlxG.sound.play('assets/sounds/explosion.mp3'); // not looped by default, autoDestroy by default
    		
    // if you want to pause/stop/etc somewhere else in your class or project, you can assign the FlxSound object.
    bckMusicPlay = FlxG.sound.playMusic('assets/music/background_play.mp3'); // looped by default
    

    I've had problems with some mp3 files if they had certain bitrate. I've found that bitrate Constant at 96kbps works. I use Audacity to convert them and use all Mono sounds to keep them smaller.

    I use ogg for my sounds unless I'm targeting flash since flash must use mp3 and all other targets can use ogg. You can use Audacity to convert those, too. If you're targeting multiple platforms, and use both ogg and mp3 you'll need to include/exclude them in project.xml accordingly. I do this:

    <assets path="assets" rename="assets" exclude="*.mp3" unless="flash" />
    <assets path="assets" rename="assets" exclude="*.ogg" if="flash" />
    

    Hope it helps.

    EDIT: I edited the code format



  • Thanks a lot! Conversion to .ogg worked!


Log in to reply