Loading external swf into a MovieClip inside your main Flash movie is a routine job and ActionScript3 allows you to do that quite efficiently. What AS3 does not provide you are the built-in functions to control loaded swf file timeline from your main movie.
I initially tried to find some built-in functions to control the loaded swf timeline but found nothing because there are none available.
After a bit of head banging, solution turned out to be a very simple one. I just needed to create a MovieClip Object to store the loaded swf reference, Type casted the loaded content of swf into a MovieClip and simply added the Event Listener for itself to control the timeline on entering every frame.
Simple! Isn’t it?
Now enough with the logic, let’s get to the actual AS3 code and see how I managed to do all of this. I have added the comments on every step to explain it further, right then:
// This is the Loader instance that will load your SWF. var swfLoader:Loader = new Loader(); // URLRequest points to your external SWF var swfFile:URLRequest = new URLRequest("external-file.swf"); //create the container Movie Clip to load swf var container:MovieClip= new MovieClip(); //create the player buttons MovieClip from library var player:swfPlayerBt = new swfPlayerBt(); // Assign an event listener so that Flash informs you when the SWF has been loaded. swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, swfLoadedHandler); //define a MovieClip object to store the reference of currently loaded swf //This will be used to access the timeline of loaded swf var currentSWF:MovieClip = new MovieClip(); function swfLoadedHandler(e:Event):void { trace("swf loaded"); //type cast the loaded swf into MovieClip object currentSWF = MovieClip(swfLoader.content); //add EventListeners for swfPlayer buttons player.btForward.addEventListener(MouseEvent.CLICK, button_forward); player.btRewind.addEventListener(MouseEvent.CLICK, button_rewind); player.btPause.addEventListener(MouseEvent.CLICK, button_pause); player.btPlay.addEventListener(MouseEvent.CLICK, button_play); //this is most important step, //Here we add an Event Listener for every frame of loaded swf //so that we could stop/play or do whatever we want on its timeline currentSWF.addEventListener(Event.ENTER_FRAME , checkLastFrame); //this function will check if swf has reached lastframe function checkLastFrame(e:Event):void { //if it the current frame of the swf equals the last frame if (currentSWF.currentFrame == currentSWF.totalFrames) { currentSWF.stop(); // trace("Simple! Done!!"); } function button_forward(e:Event):void{ currentSWF.nextFrame(); } function button_rewind(e:Event):void{ currentSWF.prevFrame(); } function button_pause(e:Event):void{ currentSWF.stop(); } function button_play(e:Event):void{ currentSWF.play(); } } //just load the swf file swfLoader.load(swfFile); //to display it, just add the loaded swf to container container.addChild(swfLoader); //set the position of player buttons player.x =200; player.y =350; //attach the swfPlayer buttons container.addChild(player);
Create a new MovieClip Symbol, and name it “swfPlayerBt”, check the ‘Export for ActionScript’ checkbox and click ok.
Now Inside this MovieClip Symbol, open Window > Common Libraries > Buttons and go to the Playback Rounded folder, and drag Play, Pause, Forward and Back Buttons from Library window to stage.
Using the Properties Panel, name appropriate buttons as button_forward, button_pause, button_play and button_rewind. Now close this MovieClip, we will call it from the Library using ActionScript code above.
in case something does not make sense to you in my above description, I have put together all this in fla file and you can download this functional fla of External swf Loader with Preloader and Play/Stop/Forward/Back Buttons.
IMPORTANT: Make sure you load AS3 external swf file and not AS2 compiled swf. Otherwise it will throw following error:
TypeError: Error #1034: Type Coercion failed: cannot convert flash.display::symbol_name to flash.display.MovieClip
Hope that helps.
Cheers!
Thank you very much you have no idea how hard it is to find something that works for an intro that’s not a timer or custom even that never seems to work. i do have one problem if your still on here. I can hear my intro but i cant see it. I’m assuming it might be off stage but I’m not sure what mc i need to center. Side note, I’m using document classes not the actions bar. and this or these mc’s are actually loading on top of a mc. each “frame” of my game is a different mc. If u can help thank you very much if not thank you very much anyways because it does work for the most part.
Yes, I’m still here but have not touched Flash for a long time.
You should be able to bring the container clip of your loaded swf to the top layer by doing this, assuming it’s hidden under other clips:
And if there are other clips being created on top of this clip, you can also bring the container clip on top dynamically. For instance you have two clips, one container, other box. Put this container clip on top of box clip after finding out the box clip level dynamically and then swap both clips like this:
Man thanks alot for this piece of infromation it really worked
Strange, I got a Type Coercion Failed error with this – both files are AS3.
Please create a new demo AS3 flash movie and test its swf.
Here is the code i updated and added, hope this will help others and request to simplify the code would be appreciate.
for each swf time line control with custom slider from this site help
http://www.kirupa.com/forum/showthread.php?370979-Timeilne-with-custom-slider-in-AS3#post2633848
Thanks Ali,
I got this code to work with my Presentation, this helps me lot, only thing is i’m repeating the function to load each file on each button click,
thanks any way,
hello. do you have the swfPlayerBt.as for me to download? i would like to see that class.
It’s just a custom button available in the library and i exported it for ActionScript to generate it dynamically. YOu can get this button from the library of available fla file.
I want to put slider in my flash animation so that at runtime I can rewind and forward the playback head. Can you please help me out
You most probably needs seek bar with the other control buttons. check this post External SWF Player with Play, Pause, Forward, Rewind Buttons and Seek Bar, demo fla is also posted there.
Can you use buttons that you design or are you locked into what buttons flash has already created? i want to design my own style of button. Is this possible?
Yes, you can easily change the skin of any buttons. In the above example, I’ve used flash buttons from the library, but you can simply replace them with you own. Just go to the library and you will see the playerClip (or something similar) where you can will see buttons aligned on stage. Just make sure you name the button instances (in the properties panel, instance_name) with the same name the already have.
Thank you so much ali, yeah its working fine now .
hello ali
this loaded swf is always display on top of everything, can we make it load behind something or in lower layer?
You can easily define the level or depth for the container MovieClip. In the above example, change the addChild(container); to addChildAt(container,0); So the last line in the provided fla will look like this:
In a real project, you might want to find out the level dynamically where you want to load your container. For instance you have two clips, one container other box. Put this container clip on top of box clip after finding out the box clip level dynamically and then swap both clips like this:
Cheers!
Hi Ali,
was wondering if you could help me with this one
I have used the above code but have added Intro to all var names as I have used this code in another part of the program and didn’t want internal conflicts. The idea is to have an external swf load, play and when it’s finished move to another frame
however this error occurs
swf loaded
TypeError: Error #1034: Type Coercion failed: cannot convert flash.display::AVM1Movie@424f2509 to flash.display.MovieClip.
at MA3RebuildMon21_fla::MainTimeline/IntroswfLoadedHandler()
text1 loaded onloaded
Hope you can help
rgds Mark
Here is the code that I have adapted from the code above:
This error “TypeError: Error #1034: Type Coercion failed:” occurs when you load AS2 compiled swf into AS3 swf, you can only load AS3 swf into AS3 flash movie.
And you can easily jump to another frame by adding one line in the following function like this:
this is code for my swf player but my swf player is not support my swf files which i had deisgnwith play pause stop next previous volume,maxmize seekbar ant swf time…….i think the problem is in timeline control…i had design my swf with movieclip into movieclip which had different code ….do u wat problem with my file….pls help me frds………..thanks in advance
this cod not working plz chk this
What is the method for stopping the swf from auto-playing? I’d like for it to load the swf to stage where a user can then press play and start it from there rather than auto.
Thanks so much this has already helped me out quite a bit with a project!!
Hi Annie,
I have updated the code for the movie to be launched by the user click. I had to make several changes in the code. I have posted it as a new article to explain it in detail. You can check it here to see how to load and unload external swf with button clicks.
Yeahhh!!!! Thank you Ali……this works beautifully!!!!!
I have been trying to solve this problem for days… and thanks to your help my mind can rest a lot easier..that headache seems to have disappeared 🙂
One problem that did seem strange was when I applied autoformat, errors about extra characters started to appear. Is this because the fla was created in CS4 and I’m using CS5?
Again thank you heaps!
regards Mark
I’m glad it helped you. The extra characters that appear must be issue with the fonts, not an issue of CS5, btw I also created it in CS5, only saved as CS4. You need to embed he fonts or use ‘device fonts’ for your dynamic text fields.
You may want to use this to unload the loaded swf.
Thank you Ali,
I am a bit of a beginner at this but most of the code has worked for my application.
I don’t understand how the player buttons are created.
I want to create just one button not the pause , play etc.
I have a quiz swf loaded and just want to be able to have a button that unloads the quiz and takes me to another page on the time line.
Can you help?
rgds Mark
For the unwanted buttons removal, you can open the fla file and go to Library and open “swfPlayerBt” clip. remove the buttons you don’t need. Also remove the code references to those buttons. For instance, if you remove Forward button, then remove this event listener:
And also remove its function:
For the button to jump to timeline, you can easily create a button just like this there to take the user to some where else in the timeline.
For instance if you use the existing button to jump to the root timeline and unload the loaded swf file, change function button_forward to this:
Just make sure you add the stop() action on first frame if you are using the above fla file and extend your timeline to frame 10 and place something there so that when you jump to frame 10, you see something.
Hope that helps.
Cheers!
Thank you Mr.Ali
God Bless You
if (currentSWF.currentFrame == certian frame number) {
increase the speed of the frame rate at that frame number
}
I humbly need your assistance with increasing or decreasing the speed of frame rate of loaded swf
i cant open ur source file in cs3 ………………
yes, it’s a CS4 .fla file. Ask some of your friend to open it in CS4 and save it as CS3. fla I use CS5, and I can only save as CS4.
im having a problem..i,e. i need to load an external swf which is in as3…and need it to be played in my as2 swf layout. with a time slider ,play, pause ,next, previous for that swf movie clip. pls give ur mail id i can sent my original files………………..pls help me
pls give ur mail id i can sent my original files……………..msksanthu@gmail.com pls help me am doing my final year college ……….
This code is AS3. You can’t add this in AS2 file. it won’t compile.
I have loaded the external swf and want to play the next frame but it’s not working. Can you check this code please ?
Here is the code:—
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.MouseEvent;
import flash.display.MovieClip;
var swfLoad:Loader= new Loader();
var urlReq:URLRequest = new URLRequest(“ball.swf”);
//addChild(swfLoad);
swfLoad.load(urlReq);
movie_loader.addChild(swfLoad);
//trace(movie_loader)
////////////////////////////////////////////
b1Btn.addEventListener(MouseEvent.CLICK, playSwf);
function playSwf(e:MouseEvent)
{
trace(“btn Clicked”);
MovieClip(this.parent.parent).movie_loader.main.gotoAndPlay(“start”);
}
Thanks Ali,
But for some reason I can’t open that fla… keeps telling me that its an unexpected file format 🙁
Any ideas?
fla is in CS4 format. You can easily ask someone to open it in CS4 and save it as CS3 fla version. I have CS5 so I can’t save it as CS3.
Hi Ali,
I’m trying to get this code to work but I keep getting stuck at the first hurdle and I’m wondering what I’m missing here…
Well, I got a couple of errors actually…
Firstly there was the missing ‘}’
“1084: Syntax error: expecting right brace before end of program” I think it is possible that I added one in the wrong place after that. (added it to line 73)
Then I get…
“1120: Access of undefined property button_forward” I get this for all the buttons and it’s very confusing 🙁
Am I doing something wrong here?
I am using Flash CS3 (Flash Player 9), and both the loader and the file to be loaded were complied using AS3.
Can you help at all?
Hi,
You can download the functional fla file from here and see yourself how the code has been structured.
To remove button errors, you need to read again instructions how you can drag buttons form library and name them inside a player MovieClip. Read the article above again and you will get what needs to be done.
Cheers!
Hi Ali,
This code has helped tremendously, but I have one issue with it. It doesn’t seem to work when I’m using the same loader to load multiple swfs, each by request. How do I apply it to a loader that is meant to hold more then one swf?
Only one swf correctly pauses, resumes. The other delete or freeze upon resuming. Any insights?
Hi Elena,
Once you have type casted the loaded swf, save its reference in different movie clips the way currentSWF is doing & then use whenever you have to. I think this should work.
You can also try separate loaders for each of your swfs if hey are not too many.
Second approach is definitely going to work.
Hi, and how do i stream the content??
spread the content on timeline rather than placing it on few frames ak preloading it, rather place your content on timeline & as it streams,it will play.
New problem .. locally the preloader works well …
But when i put it online it doesn’t work …
I’ve tried with a AS3 movie with 3,5mb and another with 45mb …
Can you help me out?? Why locally it works and on the web it gives that error??
Thanks a lot …
I checked your link with 3.5 Mb file and it loaded just fine and then played and redirected. 45 Mb is too big a file to pre load fully and you should not pre load it completely rather stream the content. Flash does very good streaming.
You can also test it in the Bandwidth profiler and loading in the flash player while developing that how would it load on different connection speeds.
Thanks, i’ll see if i can find out one that works 😉
Thanks a lot 😉
Hi Ali,
You’re right, in AS3 it works, so the external swf must be in AS2.
But i don’t know how to make this in AS2, can you help me out??
Thanks a lot.
I think you can find tutorial on AS2 easily on web, it’s easier to do in AS2. I can’t really write the code , too busy in some task, will try later sometime if you can’t find any.
Hi Ali,
Ive done as you said .. but still not working …
import flash.display.MovieClip;
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(“intro3.swf”);
vBox.removeChild(preLoader);
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) {
flash.net.navigateToURL(new URLRequest( “index.php” ), “_top”);
}
} // Close checkLastFrame
} // Close loadProdComplete
var preLoader:loader = new loader();
preLoader.x = 539,85;
preLoader.y = 404,40;
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 );
} // Close onProgressHandler
} // Close launchSWF
var container:MovieClip = new MovieClip();
var swfFile:String = ‘intro3.swf’;
var currentSWF:MovieClip = new MovieClip();
launchSWF(container, swfFile);
// Put on Stage
addChildAt(container,0);
// Button to skip Intro
skip_intro.addEventListener(MouseEvent.CLICK, botao);
function botao(e:MouseEvent):void {
flash.net.navigateToURL(new URLRequest( “index.php” ), “_top”);
}
Make sure you are loading an AS3 swf movie, and not AS2 one.
Hi,
Thanks for your reply, but i tryed and did nothing … The problem it’s that the intro was made in after effects, so i don’t know if he has a stop in the end or not …
And i only have the swf and the after effects file to edit … So i was doing new flash file with a preloader of the external swf and after the movi of the preloader plays i want it to go to index.php … Can you help me out?? This it’s the code so far (with a button to skip intro):
import flash.display.MovieClip;
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(“intro3.swf”);
vBox.removeChild(preLoader);
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();
}
}
}
var preLoader:loader = new loader();
preLoader.x = 539,85;
preLoader.y = 404,40;
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 );
}
}
var container:MovieClip = new MovieClip();
var swfFile:String = ‘intro3.swf’;
var currentSWF:MovieClip = new MovieClip();
launchSWF(container, swfFile);
// put it on stage
addChildAt(container,0);
// Button
// —————————————————————————————-
skip_intro.addEventListener(MouseEvent.CLICK, botao);
function botao(e:MouseEvent):void {
var url:String = “index.php”;
var request:URLRequest = new URLRequest(url);
try {
navigateToURL(request, ‘_self’);
} catch (e:Error) {
trace(“Error occurred!”);
}
}
Thanks a lot for your help 😉
swf does not need stop(); action at the end of it in the timeline.
Just replace the function checkLastFrame in your code with mine.
function checkLastFrame(e:Event):void {
if (currentSWF.currentFrame == currentSWF.totalFrames) {
flash.net.navigateToURL(new URLRequest( “index.php” ), “_top”);
// trace(“redirecting to another web url”);
}
Hi,
I’ve seen your tutorials and they’ve helped me a lot, but, i’m stuck
now ..
I have a external SWF (that i cannot edit) and i’ve done the loading
with the preloading, but i want to use the External swf as a intro,
so, after he plays goes to a page: index.php … How do i do this??
Please help me out, it’s very urgent !!!
Thanks a lot 🙂
Hi Pedro,
Check the above post and function checkLastFrame code, All you have to do is to redirect to a URL after the loaded SWF movie has reached the last frame. In the above checkLastFrame function, I have stopped the swf movie after reaching its last frame.
Your needed code would look something like this:
Hope that helps.
Cheers!
Thanks Ali for the slider code. I placed it and the slider shows up, but it’s not controlling the mc.
Here’s the error message:
TypeError: Error #1010: A term is undefined and has no properties.
at _800x632_D4C_sliderAS3_ali_fla::MainTimeline/mcHandler()
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at fl.controls::Slider/doSetValue()
at fl.controls::Slider/calculateValue()
at fl.controls::Slider/doDrag()
TypeError: Error #1010: A term is undefined and has no properties.
at _800x632_D4C_sliderAS3_ali_fla::MainTimeline/mcHandler()
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at fl.controls::Slider/thumbReleaseHandler()
Any ideas what I did wrong?
Thanks,
Sarah
From the errors, it seems you have not dragged the Slider component into your library from the Window > Components > User Interface panel.
Anyway, I have put together all this as a working CS4 fla file. You can Download Flash Slider sample fla file from here.
Also please use this Using Slider Component post for further comments for this issue.
Cheers!
Hi Ali,
Do you know how I would add the slider component to control (scrub) the main timeline?
Thanks,
Sarah
Hi Sarah,
If I have understood you correctly, you need a slider on stage that will dispaly different frames of a MovieClip when dragged.
You can try following AS3 code. Just create aMovieClip named mc on your stage. You can place different animations/text/artwork/nested clips or whatever on its each frame. Slider will just move through these frames when dragged.
Just Create a new Flash (ActionScript 3.0) document. and drag the Slider component from the Components panel to the your Library panel.
Place the following code on the first frame of your movie.
Cheers!