Getting sound of loaded SWF from main SWF

Posted in Flash by Flash CS3 and Flash CS4 tutorials on the 10-17-2009

Have you ever had to load a SWF file that contains an audio playback?
You may have noticed that sometimes (depending on how you play the audio file) the sound starts playing before the SWF has been fully loaded.

Here’s how to resolve this unfortunate incident using Actionscript 3.0.

Suppose we have inserted a sound file in the main timeline.
Evil! Remove it and use ActionScript to attack it.
In the FLA of SWF to load, select the audio file in the library and go to the properties.
Activate the option Export for Actionscript and add as class "MyAudio".
Click ok and go to the Actions panel.
Write

var my_audio:MyAudio=new MyAudio();
var channel:SoundChannel=new SoundChannel();
addEventListener(Event.ADDED_TO_STAGE,playAudio);

function playAudio(evt:Event):void
{
    channel=my_audio.play();
}

In this way we attack the sound on the stage using Actionscript.
The event ADDED_TO_STAGE calls the function playAudio only when it has been fully loaded from the ‘main swf’.
From main SWF, load the swf with the sound in the following way:

var loader:Loader=new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loadingComplete);
loader.load(new URLRequest("audio.swf"));

function loadingComplete(evt:Event):void
{
    var swf:MovieClip=loader.content as MovieClip;
    addChild(swf);
    //swf.channel.stop();
}

We will see that when the SWF has been loaded, the sound begins to play.
Now if we want stop the sound from main SWF, just uncomment the line

//swf.channel.stop();
Tags: , ,

Related posts

  • No related posts.


Read full article

  1. Responses to “Getting sound of loaded SWF from main SWF”

Post a Comment

captcha