Skip to main content
Known Participant
June 28, 2011
Question

External swf wont stop playing sound

  • June 28, 2011
  • 2 replies
  • 3028 views

I have created a form that has some buttons that open external swf files.  Problem is when I close the external swf the audio keeps playing.  If I turn off sounds the sound starts again when I load a new external swf.  Here is the relevant portion of code;

function playMyMovie():void{
myrequest=new URLRequest(myMovie);
myloader =new Loader();
myloader.load(myrequest);

function movieLoaded(myevent:Event):void {
  stage.addChild(myloader);
  var mycontent:MovieClip=myevent.target.content;
  mycontent.x=100;
  mycontent.y=50;

}
myloader.contentLoaderInfo.addEventListener(Event.COMPLETE, movieLoaded);
}
function removeMovie(myevent:MouseEvent):void {
myloader.unloadAndStop();
exitBtn.visible =false;
playingMovie=false;
}

Any suggestions would be appreciated.

This topic has been closed for replies.

2 replies

Inspiring
June 29, 2011

First, never use nested functions. Fix that and see if it helps.

Known Participant
June 29, 2011

Isn't that kinda the point of OOP?  Without nested functions you write the same code over and over.  Other than increasing the chance of typos, how would writing the same code 5 different times help?

Can you give me an example of a flash program that does things like wait for a swf to load that doesn't use nested functions?

Maybe I am missing something here can you please explain further?

Inspiring
June 30, 2011

"Isn't that kinda the point of OOP?  Without nested functions you write the same code over and over."

Sorry, no -  nested functions are actually against everything OOP is about.

When someone uses nested functions it is a manifestation of a gap in understanding of meaning of Function as an object. Function (method) is an entity that is meant to execute only once it is called and seize to exist after it is accomplished its mission. This means, in particular, that all the objects declared inside function are lost (garbage collected). If one introduces something in the function that holds pointers to unrelated objects like nested functions and listeners that are impossible to remove - one opens huge memory leaks in addition to something that practically cannot be referenced/executed. Again, nested functions exist as long as their parent function is in the state of execution. Scope of any function is close to any scope outside of parent function.

In AS3 there is no multithreading and function calls are execute on first come - first serve basis.

Nested functions do not prevent redundancies.

"Other than increasing the chance of typos, how would writing the same code 5 different times help?"

Function needs to be written only once and called as many times as it is needed.

"Can you give me an example of a flash program that does things like wait for a swf to load that doesn't use nested functions?"

I cannot find any well written code that uses nested functions.

your code should be something like this:

var myloader:Loader;
function playMyMovie():void {
     myrequest = new URLRequest(myMovie);
     myloader = new Loader();
     myloader.load(myrequest);
     myloader.contentLoaderInfo.addEventListener(Event.COMPLETE, movieLoaded);
}

function removeMovie(e:MouseEvent):void {
     myloader.unloadAndStop();
     exitBtn.visible =false;
     playingMovie=false;
}

function movieLoaded(e:Event):void {
     e.target.removeEventListener(Event.COMPLETE, movieLoaded);
     stage.addChild(myloader);
     var mycontent:MovieClip = e.target.content;
     mycontent.x = 100;
     mycontent.y = 50;
}

Message was edited by: Andrei1

relaxatraja
Inspiring
June 28, 2011

When you close the external swf the following will work right?

function removeMovie(myevent:MouseEvent):void {
      myloader.unloadAndStop();
      exitBtn.visible =false;
      playingMovie=false;
}

if it works, then unloadAndStop() will stop the sound too.

According to the AS3 current documentation, after calling Loader.unloadAndStop, the following happens:

    * Sounds are stopped.

    * Stage event listeners are removed.

    * Event listeners for enterFrame, frameConstructed, exitFrame, activate and deactivate are removed.

    * Timers are stopped.

    * Camera and Microphone instances are detached

    * Movie clips are stopped.

Else try with:

import flash.media.SoundMixer;

flash.media.SoundMixer.stopAll()

Known Participant
June 28, 2011

Yes, I know the documentation says it is supposed to stop the sound.  That is why I used it.  However, in this case it isn't stopping the sound.

Already tried the SoundMixer.stopAll(), and did again just now, it stops the sound at the time but when I load another swf it runs the sound from the previous swf(s) as well as the current one.

What do you mean when you say, "When you close the external swf..."?  Isn't this closing the external swf?

relaxatraja
Inspiring
June 29, 2011

can I have a look at your file relaxatraja@aol.com