AS2 → AS3: Loading & Playing External Sounds

Download Example Files

Loading and playing external sounds in AS3 is not far off from its AS2 counterpart. The code, as usual in AS3, is a little different because of the new event model, but really boils down to the same thing.

Let's take a look at the AS2 version:

  1. var s:Sound = new Sound();
  3. s.onLoad = function():Void
  4. {
  5.     trace("Song loaded.");
  6. };
  8. s.onSoundComplete = function():Void
  9. {
  10.     trace("Song done.");
  11. };
  13. s.loadSound("music.mp3", true);

And here is how to do that in AS3:

  1. var s:Sound = new Sound(new URLRequest("music.mp3"));
  2. s.addEventListener(Event.COMPLETE, doLoadComplete);
  4. var channel:SoundChannel = new SoundChannel();
  5. channel =;
  6. channel.addEventListener(Event.SOUND_COMPLETE, doSoundComplete);
  8. function doLoadComplete($evt:Event):void
  9. {
  10.     trace("Song loaded.");
  11. }
  13. function doSoundComplete($evt:Event):void
  14. {
  15.     trace("Song done.");
  16. }

As you may have noticed, there is a strange work-around in AS3 for what used to be the onSoundComplete event. I did a bit of Googling before posting this and found an interesting post by Andre Michelle on the topic. That didn't really solve my problem though, as I wanted to demonstrate how to do it in AS3, so I looked at the Flex reference for the AS2 to AS3 conversion of the onSoundComplete event and saw that it was replaced by, according to this, the soundComplete event in I went ahead and tried it out and it seemed to fire off fine on my machine, so here you have it.

Initially I had thought that the following lines of code would do the trick, but they didn't:

  1. var s:Sound = new Sound(new URLRequest("music.mp3"));
  2. s.addEventListener(Event.COMPLETE, doLoadComplete);
  3. s.addEventListener(Event.SOUND_COMPLETE, doSoundComplete);

Hence why I Googled and tried what I did above, which, as I've already stated, seemed to fix the problem. I'm no expert on sound in Flash but if it works, why question it? 😛

NOTE: I've obviously removed the "music.mp3" file from the download so just grab any .mp3 you have, put it in the same folder that the example files are in, and rename it to music.mp3.

If you found this post useful, please consider leaving a comment, subscribing to the feed, or making a small donation.


Thanks for this it really helped me

no problem, glad i could help 😛

Try this is also work.

var s:Sound = new Sound();
s.load(new URLRequest("music.mp3"));;

Lam Do,
That is the same as what I have written above. The problem with that is that it doesn't fire off the complete handlers when the sound finished.

Got your point. nice.

This worked like a dream! I did have to make some small revisions but it worked, thanks a lot!!

One question:

I saw that you used " ($evt:Event)" I used "(evt:Event)" and it worked fine. What does the dollar sign do?

@Billy: Glad it worked. The dollar sign is just my way of specifying which variables are function parameters. it is just a habit and will work fine (as you found out) without it.

Hey man, thanks for the post. It really helped me understand the SoundChannel thing better.

Nope, does not work on my windows machine with Flash CS4 and player version WIN 10,0,2,54. Seems this particular event is a bit tricky.
Back to,999999), then...

BTW, almost can't identify your captcha words.

How can I loop an external sound forever with this method?

@sshahnawaz: you'll want to change the line that says:

  1. channel =;


  1. channel =, 99999);

Where the number 99999 is the times you want it to loop (in this case a high number will make it likely that it'll just keep looping as long as the user is there).

@Christian: Unfortunately I can't test every version of Flash on every platform as that is just not reasonable 🙂 As for the captcha words, yeah, they kind of suck and i often have to reload them to get one thats legible, but it does keep spam down to a minimum which is great.

Leave a comment