Loading external MP3 files in ActionScript 2.0 and ActionScript 3.0

by Peter deHaan on March 1, 2008

in Sound, migration

The following examples show how you can dynamically load MP3 files into your SWF using the Sound class in ActionScript 2.0 and ActionScript 3.0.

Full code after the jump.

The following example shows how you can load an external MP3 file using the Sound class and loadSound() method in ActionScript 2.0:

// ActionScript 2.0
var url:String = "http://www.helpexamples.com/flash/sound/song2.mp3";
var sound:Sound = new Sound();
sound.loadSound(url, true);

The following example shows how you can load an external MP3 file using the Sound class and the load() and play() methods in ActionScript 3.0:

// ActionScript 3.0
var url:String = "http://www.helpexamples.com/flash/sound/song2.mp3";
var urlRequest:URLRequest = new URLRequest(url);
var sound:Sound = new Sound();
sound.load(urlRequest);
sound.play();

Obviously, these are very simplified examples of loading MP3s. In later examples we’ll explore things like ID3 information and setting the volume on the sounds.

{ 3 comments… read them below or add one }

1 Usama Ahmed 06.24.08 at 9:20 pm

I have tried the actionscript 2.0 example the standalone swf plays the mp3. But whenever i tried the swf on web server inside an html, it doesn’t play the mp3.

2 Roshan 03.09.09 at 10:01 pm

Great! I thought AS 3.0 was for geeks and goons! But its working, and Im neither. So Im happy!

Thanks a lot mate!

Roshan!

3 Oskar 11.29.09 at 12:25 am

works fine for me, quick & easy, thanks.

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Previous post:

Next post: