Working with Sounds, Loading Sounds

Now that your sound files are ready, let’s see how we can use them. All sound-related classes belong to the package and will be introduced throughout this chapter.

Loading Sounds

The Sound class gets access to the audio information to load the sound file. It is a subclass of the EventDispatcher class.

As discussed before, your sound can be embedded, it can be loaded as an external file from your application assets directory, or it can be downloaded from a remote server. For the latter, advise your audience to use WiFi over 3G for a better experience.

If you try to play a sound that is not loaded, you will get a runtime error. Create a listener to be notified when the loading is complete, and then play your file:

var sound:Sound = new Sound();
sound.addEventListener(Event.COMPLETE, onLoaded);
var request:URLRequest = new URLRequest(“mySound.mp3”);
// sound fully loaded
function onLoaded(event:Event):void {
sound.removeEventListener(Event.COMPLETE, onLoaded);;

You can inform the user that the file has started to load:

sound.addEventListener(Event.OPEN, onOpen);
function onOpen(event:Event):void {
trace(“sound loading”);

If it is a large file, create a listener to display the progress:

sound.addEventListener(ProgressEvent.PROGRESS, onLoading);
function onLoading(event:ProgressEvent):void {
// display the percentage loaded

On Android devices, it is important to check for errors, particularly if the file is served from a remote server. Inform your user if there is a network issue:

sound.addEventListener(IOErrorEvent.IO_ERROR, onError);
function onError(event:IOErrorEvent):void {
trace(“sound cannot be loaded”, event.text);


Streaming is the process of playing part of a sound file while other parts are loading in the background. The advantage is that you don’t need to wait for the whole file to download before playing. In addition, you can play very long tracks without memory constraints.

The audio files must be located on a streaming server. The quality of the server is an important factor to a good experience: 128 kbps is sufficient for audio, but a musician can detect artifacts in high frequency for MP3 encoding. Encoding your audio at 192 kbps is a good compromise.

Requesting the file is the same process as before.

You can start playing the audio as soon as there is enough data in the buffer. Buffering is the process of receiving and storing audio data before it is played. The default buffer time is 1,000 milliseconds. You can overwrite the default using the SoundLoaderCon text class.

In this example, the buffer time is changed to five seconds:

var sound:Sound = new Sound();
var request:URLRequest = new URLRequest(“myStreamingSound.mp3”);
var context:SoundLoaderContext = new SoundLoaderContext(5000, true);
sound.load(request, context);;

The SoundLoaderContext class is also used for security checks when loading sounds, but it may not be required in AIR.

Streaming MP3 files is buggy when it comes to midstream bit rate changes, a method often used by streaming services such as Internet radios. The audio sounds like it speeds up or is broken in chunks because it uses the bit rate declared at the start of the stream, even after a change.