Unloading swf using the unloadAndStop() method, but video sounds remain audible

5.9k Views Asked by At

I have tried many approaches to unloading my swf, but to no avail, the video sounds within my laoded swf keep playing even once the swf has been loaded.

I have created a universal loader as follows:

var loader:Loader = new Loader();

I then load various swf's into a movie clip named mov_contentLoader, for example the video swf is loaded as follows:

loader.load(new URLRequest("video.swf")); //assign SWF url to loader
mov_contentLoader.addChild(loader); //add loaded content to movi clip

I then have a generic "exit" button, based on the state of the application, certain windows are closed, swf's are unloaded etc. For my video.swf file, when the exit button is clicked, I call the unloadAndStop(); method on the loader as follows:

loader.unloadAndStop(); //unload all content, do some garbage cleanup
mov_contentLoader.removeChildAt(0); //just to be safe, a second layer of reassurance ??

The SWF is unloaded, but for the life of me I can NOT get the sounds to stop! I even resorted to adding stage listeners on the video.swf to listen for an exiting state, but that still did not stop video sounds.

If I load the video.swf in again and play a different video, the two soundtracks play ontop of each other.

Any guidance, as always, is greatly accepted and appreciated.

Simon

4

There are 4 best solutions below

1
On BEST ANSWER

After much research I have solved this problem. In the hope that this will make someone's life a lot easier, here was the issue:

The unloadAndStop(); method removes any event listeners, instances, objects etc from the loaded SWF. In my case I had videos, and IF the video was still playing when the user exited (and the loaded unloaded the content) the sounds would carry on. It seems that one thing the unloadAndStop(); function does NOT do is stop all sounds.

The solution? I attached an event listener to my loader, and after the content was successfully UNLOADED, I called the SoundMixer.stopAll(); function which ensured no sounds carried on playing! This DOES however mean that if you had ambient background sounds all along, they would stop too. You could go even further and get the current time of your ambient sounds and simply "restart" them immediately from that point in time.

My simple vent listener looks like this:

myloader.contentLoaderInfo.addEventListener(Event.UNLOAD, cleanUp);

function cleanUp(e:Event):void{
   SoundMixer.stopAll(); //stop all sounds...
}

I trust this will help someone! It's a little "hacky" but it does the job just fine.

Kind regards, Simon

6
On

The appropriate way to handle this is to program a destroy function into whatever loaded content you have, and then call it before you unload that content. In the destroy function the loaded swf should be responsible for its own business in regards to...

  • Stopping playing sounds.
  • Closing any open streams (like streaming video).
  • calling stop on its timelines
  • etc...

SoundMixer.stopAll() is in no way an appropriate solution. Rather, it can be if you are the one setting the parameters of what is "acceptable" as a jarring user experience, but if you are writing a 3rd party swf that is to be loaded into someone else's application you'll most certainly be responsible for cleaning up your own sound mess, and SoundMixer will kill not just your sounds, but the sounds from the loader, and that is one sure fire way to anger your hosts.

0
On

The eventlistener that listens for the Event.UNLOAD and then calls the function which pulls out the 'SoundMixer.stopAll()' function is a decent workaround. This doesn't work if you are loading your swf from another domain than the one your container swf is on. I'm pretty sure it triggers a sandbox violation. I do not have a solution for that - and the best solution anyway is the one mentioned above, mentioned by scriptocalypse.

But I'm in a situation where I am on different domains AND I have no control over the SWF files - so I can't put 'destroy' functions into them. I'm going crazy.

0
On

In your video.swf, you have to write something like this:

this.addEventListener(Event.REMOVED_FROM_STAGE, stopSound)

function stopSound(e:Event):void
{
    my_flvPlayer.stop();
}