AS3 Tutorial: How To Control Sound from an Externally Loaded SWF
- February 11th, 2010
- Write comment
First I talk about motorcycles, then I talk about iPhone applications, I brush up on a little bit of fashion, and now I turn to my inner geek to talk about some programming. I know programmers don’t want to read a bunch of unnecessary stuff, so I’ll get right into it.
Have you ever loaded an external .swf file that contained audio and couldn’t control the sound?? Well I have, and it drove me crazy for hours (days even). The solution is actually a fairly simple one. First off, let’s create our variable for our SoundTransform() class and load our external swf file:
private var new_var:SoundTransform();
private var loaded_swf:Loader = new Loader();
loaded_swf.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderHandler);
loaded_swf.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, loaderHandlerError);
loaded_swf.load(new URLRequest("swf_files/movie_name.swf")); //in my case, it was a swf from some third party server
Then let’s add the handler and error functions:
private function loaderHandler(e:Event):void {
var mc:* = e.target.content;
addChild(mc);
}
You don’t have to add the error handler function but it’s good practice, last thing you want are errors popping up everywhere. Looks tacky.
private function loaderHandlerError(e:Event):void {
trace(e, "File Unable To Load");
}
In your handler function, instantiate a new SoundTransform(); class and set the volume property between 0 and 1 (0 being muted). Then, last but not least, use the soundTransform property of your target.content (in our case, mc) and set it to your SoundTransform(); class (in our case new_var):
private function loaderHandler(e:Event):void {
var mc:* = e.target.content;
addChild(mc);
new_var = new SoundTransform();
new_var.volume = 0;
mc.soundTransform = new_var;
}
Then compile, you won’t hear any audio. Hopefully this will come in handy for someone like it did for me.
