Skip to content


[AS 3] Actionscript 3.0 Load External .mp3 Audio

So, for a little game that I have been destructing I was in the need of loading some simple sound effects. I grabbed an audio file from Flashkit.com and created this extremely simple/basic package:

package {
    import flash.display.Sprite;
    import flash.media.Sound;
    import flash.net.URLRequest;
    import flash.media.SoundChannel;

    public class LoadAudio extends Sprite {
        private var snd:Sound = new Sound();
        private var channel:SoundChannel = new SoundChannel();

        public function LoadAudio() {

            var req:URLRequest = new URLRequest("audio/PowerUp.mp3");
            snd.load(req);
            channel = snd.play();
        }
    }
}

I placed the above code in a file appropriately named: LoadAudio.as and set LoadAudio as the document class of my .fla file.

Posted in Actionscript 3.


4 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. james says

    …and for looping audio (as an alternative to snd.play(0,xNumofTimes), add the following:
    channel.addEventListener(Event.SOUND_COMPLETE,loopSound);

    function loopSound(e:Event):void{
    channel = snd.play();
    }

  2. fred says

    if I put a web url it says Error opening URL ‘http://www.newgrounds.com/audio/download/245921′
    Error #2044: Unhandled IOErrorEvent:. text=Error #2032: Stream Error.
    at LoadAudio()

    it is a download url however

  3. Daniel says

    I get this same error if I use it in flash. Any Ideas as to what is wrong?

  4. admin says

    Daniel & Fred,

    This may be because of a the Flash Security Settings – try and use a relative path to the audio file rather than an absolute one.

    -M



Some HTML is OK

or, reply to this post via trackback.