How to remove or unload an externally loaded swf file from your project?
It happenes a lot that you try to load an external SWF file into your project. maybe in your portfolio or some swf movies or sound files. The problem I am going to talk about here is about the common problem of unloading the swf files!
you see, if you have loaded an swf file which has some music on it playing constantly, when you try to unload/remove that swf, the sound keeps playing! (not just sounds, maybe some Event.ENTER_FRAME or other listeners are still working)
to avoid this problem, the best solution I can think of is to add a REMOVED_FROM_STAGE listener in your external swf file so it can listen to when it’s removed from the stage so it can kill the running processes inside itself.
simply add it like this:
1 2 3 4 5 6 | import flash.events.Event; this.addEventListener(Event.REMOVED_FROM_STAGE, onStageRemoved); function onStageRemoved(e:Event):void { // kill all listeneres and running processes here. } |
Regards,
Hadi

February 11th, 2010 9:25 am
Hi!
Thanks for posting this tip! I struggled with this problem like so many others and non of the other solutions out there worked, but this one did.
Thanks again and keep up the good work!
Regards
Kriss
August 5th, 2010 10:53 pm
Can you help me with this ?
Here is my code for my current video player. I want to use this in a flip book. When the page is turned I need the video to stop playing.
Below this code is my code for the video player. Where do I put the remove from stage code?
import flash.events.Event;
this.addEventListener(Event.REMOVED_FROM_STAGE, onStageRemoved);
function onStageRemoved(e:Event):void
{
// kill all listeneres and running processes here.
}
My code:
function videoOne(event:MouseEvent):void{
pubPlayer.source=”bill.flv”;
}
function videoTwo(event:MouseEvent):void {
pubPlayer.source=”jeff.flv”;
}
bill.addEventListener(MouseEvent.CLICK,videoOne);
jeff.addEventListener(MouseEvent.CLICK,videoTwo);
August 7th, 2010 10:11 pm
if you’re coding in timeline, put that code on the first frame of your external SWF file.
December 8th, 2010 11:22 am
I must have spent five hours trying to fix this problem. I realized my removeEventListener was not working, but could not figure out how why; even when I figured out that removing the object from the stage (and then adding it later) was causing the problem, I could not find a simple way to correct it. Your code really helped. Sometimes I wish Adobe would give more examples; a lot of the time, their “documentation” is so vague that it’s useless. Anyway, thanks much!
December 8th, 2010 5:59 pm
I’m glad it helped
December 26th, 2011 8:54 am
Hi guys,
Thanks once more for sharing your time and expertise.
I’ve been totally unsuccessful in solving this problem on “removing external SWF files”. After trying many other codes, I used yours, but due to my extreme lack of even basic knowledge in AS, I couldn’t make it work. The fact is that I have no clue of what to write in the “kill all listeners and running processes here” area…
The FLA I created to generate my SWF file has no code in it whatsoever. It has just one frame, where I place a Flash component called “Slide Show Pro”. I load it into your “Rose” template, and it works just fine, but of course, videos keeps playing when one goes to a different page. You can see it in a hidden page I have in the site: http://www.greenfieldbmc.com/indexVideo.html#/GF_Video-Gal/
Could you please help with this? I would certainly be very thankful.
Thanks in advance, and once again, Merry Christmas and best wishes for next year. Take care,
Nomar
December 28th, 2011 12:32 am
Nomar, I’m not sure how the “Slide Show Pro” works as I never have used it myself, but if it’s written with OOP standards, it should have a method, or property or something that would let you stop its functionality, right? if you see a method call in the component that let’s you stop the slideshow, then all you have to do, is to copy and paste the code in this blog, to the first frame of your FLA and call that method from the “onStageRemoved” function.
Hadi
December 30th, 2011 11:19 am
Thanks, Hadi, I’ll try to find how it is written.
Best regards,
Nomar