How to play a sound in Actionscript 3 that is not in the same directory as the SWF?

11,389

Solution 1

Well, I've just done a test by putting an mp3 in a directory: soundTest/assets/song.mp3 then creating a swf that calls the mp3 in another directory: soundTest/swfs/soundTest.swf and when I use var path:String = "../assets/song.mp3"; then it compiles with no errors.

What is your actual directory structure?

Solution 2

Unless you're going to put a full url, don't use http:// or file://

Sound can load an mp3 file from a full or relative url. You just need to make sure your url is correct and valid.

For example, if the full path to the file is http://www.something.com/assets/the_song.mp3, a path of "/assets/the_song.mp3" would work.

Solution 3

You should really download httpfox for FireFox. This SNIFFER allows you to see what data is flowing through the browswer. You can see the files its loading, including the paths to each, and you can even sniff POST and GET variables. This will show you where the files are being pulled from and based off of that you can fix your relative paths accordingly.

https://addons.mozilla.org/en-US/firefox/addon/6647

Important:

All external assets called from the SWF are relative to the html file loading them when loaded on the web, not the SWF. The only exception, and this is something that started with AS3, FLV's are relative to the SWF, not the HTML document loading the SWF like every other asset. This is why SNIFFERS are an important tool, I scratched my head for a while until I noticed the URL in the sniffer was calling a weird path.

Below is how you can load sound.

var soundRequest:URLRequest = "path/to/file.mp3";
var s:Sound = new Sound(soundRequest);
var sChannel = s.play(0, int.MAX_VALUE); //Causes it to repeat by the highest possible number to flash.
//Above starts the sound immediatly (Streaming);

//Now to wait for completion instead, pretend we didnt start it before. s.addEventLister(Event.SOUND_COMPLETE, onSComplete, false, 0, true); function onSComplete(e:Event):void { var sChannel = s.play(0, int.MAX_VALUE); //Causes it to repeat by the highest possible }

Share:
11,389
ashu
Author by

ashu

Game Developer

Updated on June 04, 2022

Comments

  • ashu
    ashu about 1 year

    I have a project with a bunch of external sounds to a SWF. I want to play them, but any time I attempt load a new URL into the sound object it fails with either,

    Error #2068: Invalid Sound

    or raises an ioError with

    Error #2032 Stream Error

    // Tried with path prefixed with "http://.." "file://.." "//.." and "..")

    var path:String = "http://../assets/the_song.mp3";
    var url:URLRequest = new URLRequest( path );
    var sound:Sound = new Sound();
    sound.addEventListener( IOErrorEvent.IO_ERROR, ioErrorHandler);
    sound.addEventListener( SecurityErrorEvent.SECURITY_ERROR, secHandler);
    sound.load(url);
    
  • ashu
    ashu over 14 years
    Sorry I wasn't clear in the example at the top. If I try your suggestion of path = "../assets/the_song.mp3" I receive and error of "Error #2068: Invalid Sound". (I also just tried "/../assets/the_song.mp3" which resulted in the same error.)
  • ashu
    ashu over 14 years
    I don't have any compile-time errors either but I get a run-time error (and the sound doesn't play) when I try either of the following: path = "http://../assets/the_song.mp3" // ioErrorEvent, Error #2032 Stream Error path = "../assets/the_song.mp3" // Error #2068: Invalid Sound
  • ashu
    ashu over 14 years
    Note those are two samples, looks like returns don't get put into comments. Just want to stress I'm receiving a run-time error, not a compile time error.
  • CodeIT
    CodeIT over 14 years
    Hmmm, I am also NOT getting any run-time errors. The only difference between my example code and yours, is that I am not listening for any IO or Security errors. Could you try it with those stripped out?