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.

Tagged with:
 

9 Responses to Loading external MP3 files in ActionScript 2.0 and ActionScript 3.0

  1. Usama Ahmed says:

    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 says:

    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 says:

    works fine for me, quick & easy, thanks.

  4. Mudasser ajaz says:

    i tried this,,working good

  5. Ralastas says:

    How would I put a loop function into the actionscript itself?
    I’ve tried looping using Adobe Flash CS4, but for some reason it just won’t loop.

  6. Ralastas says:

    Also, I forgot to mention, I’m using Actionscript 3.0

  7. hadi says:

    Hello.Please try to solve my problem
    //Action//
    var url:String = “http://sound.nasr19.ir/uploads/1319552123.mp3″;
    var S:Sound = new Sound();
    S.loadSound(url, true);
    //in this part the sound does not repeat. What is the problem?//
    s.onSoundComplete = function ()
    {s.start (0,999)};

    timerinProgress = false;
    musicOn = true;
    playing = true;
    newPosition = 0;
    onEnterFrame = function () {
    if (advance) {
    if (s.position/1000+1<s.duration/1000) {
    if (playing) {
    newPosition = s.position/1000+1;
    s.stop();
    s.start(newPosition, 1);
    }}};
    if(s.getBytesLoaded() == s.getBytesTotal()){
    trace("the sound is loaded")
    } else {
    trace("the sound is not loaded")}};

    //in this part of cod the Button Does not work (can not play && pause). What is the problem?//
    //Button cod//
    on (release) {

    if (!_root.playing && _root.newPosition < _root.s.duration) {
    _root.s.start (_root.newPosition, 1);
    _root.playing = true;
    } else if (_root.playing) {
    _root.newPosition = _root.s.position / 1000;
    _root.s.stop ();
    _root.playing = false;
    }}
    the sound load But Button Does not control && play and pause the sound
    help mi! Thanks and waiting .As2

  8. Natan says:

    works fine for me too, swf and swf in html

    [flash cs5, AS2]

    thanks! :)

  9. Olker says:

    Nice. Short and practical explanation. Thank you!

Leave a Reply

Your email address will not be published.

You may 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="" highlight="">

Spam Protection by WP-SpamFree