When we load external swf files in Flash Movies, they may take some time to load and it’s better to place a preloader in the main movie to inform user that content is loading. It’s a bit tricky to set up preloader for external swf using ActionScript3 in Flash.
Following example will show how you can load external swf file, create a preloader and show it before displaying the loaded swf file to the user:
function launchSWF(vBox, vFile):void{ var swfLoader:Loader = new Loader(); var swfURL:URLRequest = new URLRequest(vFile); swfLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler); swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadProdComplete); swfLoader.load(swfURL); function loadProdComplete(e:Event):void { trace("swf file loaded"); //remove the preloader from container clip vBox.removeChild(preLoader); // add the loaded swf to container clip vBox.addChild(swfLoader); currentSWF = MovieClip(swfLoader.content); currentSWF.gotoAndPlay(1); currentSWF.addEventListener(Event.ENTER_FRAME , checkLastFrame); function checkLastFrame(e:Event):void { if (currentSWF.currentFrame == currentSWF.totalFrames) { currentSWF.stop(); // trace("stopped"); } } } var preLoader:loader = new loader(); //position the loading bar preLoader.x = 155; preLoader.y = 185; vBox.addChild(preLoader); function onProgressHandler(event:ProgressEvent){ var dataAmountLoaded:Number=event.bytesLoaded/event.bytesTotal*100; preLoader.bar.scaleX = dataAmountLoaded/100; preLoader.lpc.text= int(dataAmountLoaded)+"%"; trace(preLoader.bar.scaleX ); } } //create the container clip var container:MovieClip = new MovieClip(); //define the swf file to load var swfFile:String = 'external-file.swf'; //define a MovieClip to store current swf ref var currentSWF:MovieClip = new MovieClip(); //call the function to load swf, provide container clip and external file launchSWF(container, swfFile); //put it on stage addChild(container);
You can download this functional external swf preloader fla file from here.
Hope that helps.
Cheers!
Hi, this is awesome, thanks for sharing.
It works almost perfectly, my only problem is that the sound starts playing while loading… Is there a way to stop the sound or turn it off while the swf is loading ?
Thanks in advance for helping
Place this code at top:
See more at: https://www.parorrey.com/blog/flash-development/external-swf-player-with-play-pause-forward-rewind-buttons-and-seek-bar/
Hey this is awesome thank you, I got everything to work but I can’t unload it. I have it so that I use the unloadAndStop function on the frames that the external swf shouldn’t appear and it worked before when I used a simple external loader code but with your code it doesn’t want to work.
When I use swfLoader.unloadAndStop(); it tells me “access of undefined property swfLoader.
If I type in container.unloadAndStop(); it doesn’t give me an error but it also doesn’t unload it.
Any help would be appreciated and thank you very much for sharing your code
Please check this post for unloading external swf file. I have shared the code and fla file. swfLoader.unloadAndStop(); should do the job. Just make sure where you have added this code, swfLoader is available there. Defining swfLoader outside the function might help.
i am VERY new to this, so this is probably a very simple question, but, how do I get this into my .swf file? I have creates a looping .swf slideshow and I want to put this in front, while loading, but not come back up every time the slide show loops back to the beginning as all the other codes I have tried did. Thanks!!
I think I got it.
I changed this line:
currentSWF.gotoAndStop(2);
and now it seems it works 🙂
Thanks!
Hi,
Thanks a lot for this code.
I tried to use it and it works but in the way that my swf file I was loading starts and plays till end immediately.
Is there some way to stop it?
Thanks!
Ali,
How can I remove the swf loaded? I’m using a buttom but can’t manage how to do this.
Thanks!
This is specific code in flash:
You can check this post to see how it’s done. Demo fla is also available there.
Hi Ali,
I’ve just tried implementing your code and it is working great to load the external swf. However once the swf is actually loaded none of its functionality works correctly.
I even tried using your template and just dropping in my swf, with the same outcome
Alex, Make sure that you are loading AS3 swf file. AS2 swf cannot be loaded into AS3 compiled movie.
Hi Ali,
Alex made a very good observation, anytime it load an swf with some functions , the functions dont work.
When it load a simple swf with no functions, no problems… There is something hapening…
thanks for the help!
That might well be the case but whatever I have tested, it worked well for me and I did use some advanced AS3 swfs with lot of functions and code in them. There may be some code issues in those swfs actionscript.
Thanks I’ll try it 🙂
thanks for this post.. but I was wondering – what if you called the pre-loader with the Flashvars parameter set – how would you pass this information on to the SWF you’re loading?
Hi Stewart,
One way of passing the var value to loaded swf is by this (using the above example):
Where the var_value is the loaded value in the main swf movie from the html page via FlashVars.
This is how you get the variable inside swf:
Thanks for providing this – it’s been really helpful. However. My flash project gets a value from a php/mysql query. Basically – I pull a competition code from a database, and based on what that code is, I reveal a specific prize in my flash file. The problem with using this preloader is I think I’m loosing that value.
how would I pass on that value from the pre-loader to the .swf being loaded?
this code gets the value passed to the preloader:
var keyStr:String;
var valueStr:String;
var paramObj:Object = LoaderInfo(this.root.loaderInfo).parameters;
for (keyStr in paramObj) {
valueStr = String(paramObj[keyStr]); // holds the name of the prize image
}
so how does valueStr get to my .swf file?
thanks.
I’ve try with several swf files ^^’ Maybe I have to change something about the weight in the code ? Video size is about 70 to 100 MB.
larger swf file should not be a problem. try this Multiple SWF Slideshow fla file from this blog post
I have checked it, the swf file does not play until it’s fully loaded.
Thanks to answer but I’ve try it and that does the same ^^’ The video begin before the end of the load :S
Have you tried it with some other swf file? May be the issue with within your swf file.
Hello, really nice tutorial 🙂 I’ve just have a little problem, the video loaded begin before the end of the load. There is the loading bar in progression but I can hear the video sound begin and when the load finish, the video don’t start from the begining ^^’
Here’s my link to see what I’m talking about ^^
http://www.newgrounds.com/portal/view/582057
Please check this similar blog post, It uses the same preloader logic and loads external swf to play from a specific frame.
Hi friend,
Thanks to you for the preloader, I am going to use it in my game. I am facing one problem, that is, I have the preloader in the document size, I want to make it in the size of my staze of the document, like 800x580px.
Please tell me where to change this.
You can easily change the size of the preloader movie clip or better still change the width and height of nested logo graphic symbol inside preloader movie clip.
Cool, Thanks.
Hi, I can’t seem to download this file. could you check the link or email it to me please. I’m looking for a AS3 preloader that will load the data of slideshow pro component inside a swf container.
Regards
Hi Derek,
Link is fine. try again,
It is an AS3 compiled swf but with a Flash 9 player… and it is working… http://unitedminingequipment.com/News_cnf.php
It is a large swf because it has to embed chinese character sets to display the XML file properly…
Jeff
I just checked the url, and it appears your slideshow is loading just fine, although i do get the error after downloading the file and running locally.
Hi, I am trying out this nice preloader (THANK YOU! Every since AS2, Adobe has made basic functionality needlessly complex!).
It is loading the swf but giving this error:
swf file loaded
TypeError: Error #1034: Type Coercion failed: cannot convert UMEContactCN@538680b1 to flash.display.MovieClip.
at Function/()
It appears to be working, just not sure what this might mean..
Hi jefferis,
You are attempting to load an AS2 compiled swf into AS3 swf. This won’t work, try only AS3 movie.
You are right about Adobe making things needlessly complex. Flash 5 was so simple! 🙂
On another note, once you get hold of AS3(it takes some time), it becomes very logical and much easier than AS2.
Cheers.
I really appreciate the post and being able to find you – kudos on the SEO.
I remember searching fro as3 preloaders 2 years ago and finding dozens, Now It seems harder than ever. I find more as2 post than as3. Seems to me more designers are focus on JS libraries than flash.
Hi Andre,
Being an SEO myself, it helps in tutorial copywriting on such topic.
I’m glad I did well on both counts, SEO & Flash.