control sound playbacks

10 10 2007

I had a mp3 file that I wanted to play once, stop it if needed, and when it’s done playing, just get rid of it. I know in AS2 it could be as easy as dragging the file into an empty MovieClip (from the library) and monitor the playhead with an onEnterFrame listener. However, everything in AS3 involves just a tad more code. Here is how I ended up doing in AS3.

[+] import the mp3 file into the library. Give it a linkage name “com.interactiveSection.MySound” (a customized class) which extends the class.

[+]In my class “com.interactiveSection.MySound”, the constructor simply supers the two arguments the super class takes. The code for the class looks like the following:

    public class MySound extends Sound
        public function MySound ( stream:URLRequest=null, context:SoundLoaderContext=null )
                super(stream, context);

[+]In the class where I use the MySound object, make a new MySound object with no arguments, since I am not loading any external sound file. The snippet of code looks like the following:

mySound_snd = new MySound ();

[+]Start the playback of the mySound_snd. Make sure to store the return value (a SoundChannel object created by starting playing mySound_snd) in a variable so we can access it later. The snippet of code looks like the following:

mySound_sc =;

[+]Stop the playback of the mySound_snd. Since Sound object doesn’t have a stop method any more, we have to do this by telling the SoundChannel object to stop. The snippet of code looks like the following:


[+]Create the handler method for when the sound is done playing. We need to tell mySound_sc to listen to the SOUND_COMPLETE event right after when it is been created, it will then call the handler method when the event is fired off. The snippet of code looks like the following:

mySound_sc.addEventListener (Event.SOUND_COMPLETE, soundCompleteHandler);
protected function soundCompleteHandler(evt:Event):void
    //code to get rid of mySound_snd

I found the above event handler has to be attached onto the SoundChannel object, which was created by the playback of the Sound object, otherwise it will not work. I also found the following will NOT work: I had enterFrame handler in which I compared mySound_sc.position against mySound_snd.length. For some weird reason, the position of the SoundChannel will never catch up with the length of the Sound object. (In my case, it always had a gap of around 200 milliseconds. 😦 )

Refer to the discussion on Google groups for more detailed code examples.

[+] updated on 10/17. The day before yesterday, Adobe just updated the page on livedocs to emphasize that the event handler for SOUND_COMPLETE has to be attached to SoundChannel object. There you go.




One response

18 03 2008

I have my own blog, but yours is just amazing. Love it.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: