Skip to content
WingEraser edited this page Mar 15, 2014 · 5 revisions

In this guide you learn how to use the Audio API. Flixel provides methods to playback small sound effects as well as stream larger music files directly disk.

##Audio Formats

Platform .wav .mp3 .ogg
Android
Desktop (LWJGL)
iOS
HTML5*

*Note: supported file formats depend on the browser used.
*Note 2: libgdx uses SoundManager made in Flash to play audio.

##Sound vs. Stream The audio system offers two functions for loading: FlxG.loadSound() and FlxG.playMusic(). The difference between these two:

  • FlxG.loadSound() loads an entire sound into memory. It should be used for small audio samples that may be used repeatedly such as a character shooting a gun.
  • FlxG.playMusic(), sounds that are longer than few seconds is recommended to stream it from disk instead loading it into RAM.

##Audio usage All audio functions go via FlxG, the Global helper class. If you want more control over an individual sound object, you need to create an FlxSound object.

###Sound There are several ways to play sound. This method will create a sound object (FlxSound), cached it and play it.

// path, volume on 1, looped, auto destroy, auto play.
FlxG.play(SndShoot, 1, false, false, true);

You may want to load the sounds before the game starts instead creating sound object on runtime.

String SndShoot = "path/sound.ogg";
// path, volume on 1, looped, auto destroy, auto play.
FlxSound soundShoot = FlxG.loadSound(SndShoot, 1, false, false, false);
soundShoot.play();

###Music Playing music is done with one method.

// path, volume, looped, auto destroy.
FlxG.playMusic(Music, 1f, true, true);

###Controlling Audio

FlxG.resumeSounds() Resume playing existing sounds.
FlxG.pauseSounds() Pause all sounds currently playing.
FlxG.mute = true Mute the sound if set to true.
FlxSound.resume() Unpause a sound.
FlxSound.pause() Pause a sound.
FlxSound.stop() Stop a sound.
FlxSound.fadeOut() Make the sound fade out over a certain time interval.
FlxSound.fadeIn() Make the sound fade in over a certain time interval.
FlxSound.setVolume() Set the volume of the sound.

###Disposing Audio If the argument auto destroy is set to false, you need to dispose the sound manually.

soundShoot.destroy();

Or via path of the sound.

FlxG.disposeSound("path/sound.ogg");

Destroy all sounds, even if they are flagged to survive.

FlxG.destroySounds(true);

Examples: