Looping Sound in Flex and AIR

This is a short example of how to play an audio clip and loop it in Flex, and works with AIR. Some of this code is taken from the Sound class example in the Adobe Flex Language Reference docs.

You can paste this into pretty much any function, like an init():

var soundReq:URLRequest = new URLRequest("someSound.mp3"); 
var sound:Sound = new Sound(); 
var soundControl:SoundChannel = new SoundChannel(); 
sound.addEventListener(Event.COMPLETE, completeHandler);
sound.addEventListener(Event.ID3, id3Handler);
sound.addEventListener(IOErrorEvent.IO_ERROR, ioSoundErrorHandler);
sound.addEventListener(ProgressEvent.PROGRESS, progressHandler);
sound.load(soundReq); 
soundControl = sound.play(0, int.MAX_VALUE);


Notice the line:
sound.play(0, int.MAX_VALUE);
The second parameter is the number of times to loop. Set it to zero to play the sound one time. There’s no real way (that I have found) to play the audio infinite times, so I put in int.MAX_VALUE which is a 32-bit signed integer of the value 2,147,483,647. Sure this won’t loop forever, but given a one second audio clip it would loop for about 68 years!

Here’s the handlers to go along with it:

private function completeHandler(event:Event):void {
    trace("completeHandler: " + event);
}
 
private function ioSoundErrorHandler(event:Event):void {
    trace("ioSoundErrorHandler: " + event);
}
 
private function id3Handler(event:Event):void {
    trace("id3Handler: " + event);
}
 
private function progressHandler(event:ProgressEvent):void {
    trace("progressHandler: " + event);
}

I’m still new to Flex so if anyone notices anything that isn’t right, please let me know. Also if anyone is looking for a decent BlazeDS tutorial, I’ve found one here. Also been reading Programming Flex 3: The Comprehensive Guide to Creating Rich Internet Applications with Adobe Flex which isn’t bad, if anyone else recommends a book let me know.


4 Comments

  1. Ansury says:

    Usually a 0 (rather than int.MAX_VALUE) means “forever”, and since that’s the default parameter value for play() I’d imagine that’s the case here as usual. Still, I guess 65 years means the point is moot. :)

  2. Ansury says:

    Scratch that. Adobe, you’re killing me. The “usually” I mentioned above is not the case here. Apparently 0 == 1 to whoever designed Sound.play(), so you do have to use int.MAX_VALUE for a never ending repeat!

  3. Nick says:

    Yea, they do stuff that doesn’t make a whole lot of sense sometimes.

  4. avrs says:

    it’s weird, I used SOUND_COMPLETE to loop my sound and the looping wasn’t seamless compared to AS2. I used your above example + SOUND_COMPLETE. Now the sound will loop 68 years, then it will execute the sound complete which will make it play again for 68 years (might have a slight noticable gap but still better than stopping).. and so on :D

    var mySoundChannel = mySound1.play(0, int.MAX_VALUE); //play with maximum possible looping

    mySoundChannel.addEventListener(Event.SOUND_COMPLETE, soundComplete); //add an event listener to check if sound has completed looping

    function soundComplete(event){
    trace(“Looping…”);
    mySoundChannel = mySound1.play(0, int.MAX_VALUE); //play with maximum possible looping
    mySoundChannel.addEventListener(Event.SOUND_COMPLETE, soundComplete); //add an event listener to check if sound has completed looping
    }

    C’mon Adobe fix that SOUND_COMPLETE glitch already or give us true looping.

Leave a Reply