Skip to content

Working with sound

Mark Knol edited this page May 30, 2013 · 25 revisions

Play a sound

To play a cross-platform sound, you need to save/encode your sound to different extensions (like this: mySound.m4a, mySound.mp3, mySound.ogg). When grabbing the sound out of the assetpack, you don't need to add the extension, Flambe will use the right sound for the right platform. More on loading (external) assets, read Working with assets.

pack.getSound("mySound").play();

Tip: Convert sounds using Audacity
Make sure you've also installed LAME MP3 encoder and the FFmpeg import/export library.

Controlling playback

The play() and loop() of a Sound instance returns a Playback instance. You can use it to manipulate the volume, get the duration or pause,play and stop the sound.

var mySoundPlayback:PlayBack = pack.getSound("mySound").play(); // play a sound
var mySoundPlayback:PlayBack = pack.getSound("mySound").loop(); // loop a sound

Alter sample volume

Volume values are from 0-1.

// directly set volume when you start the sound to half volume (0.5)
pack.getSound("mySound").play(0.5);
// .. or set the volume afterwards
mySoundPlayback.volume._ = 0.5; // set volume to half volume
// .. or fade the sound in one second to half volume
mySoundPlayback.volume.animateTo(0.5, 1); 

To understand the animate and underscore syntax, read Working with values.

Pause/play/stop

mySoundPlayback.paused = true; // set to pause
mySoundPlayback.dispose(); // stop sound and remove

Global volume

If you want to alter the global volume which affects all samples, or want to mute all sounds, you should do that into the System.

System.volume._ = 0; // mute all sounds
System.volume._ = 1; // unmute
System.volume.animateTo(0.5, 1); // fade global volume to half volume (0.5) in 1 second.

Support

  • Not all Android devices seems to play sound. On slow/older android devices sound are laggy or don't play instantly which could cause timing issues.
  • On iOS5 (like iphone4) sound require user input. This is not build in Flambe, so it won't work. On higher versions sounds seems to work very good.
Clone this wiki locally