18
2011Multiple External SWF Slideshow – Loading & Playing Mupltiple External SWFs in AS3 Flash Movie
Tags: Flash AS3
I have previously posted several blog posts for loading external swf file and controlling its timeline by adding play/pause/forward/rewind player buttons. Some days back, I got a related and interesting question which involved loading and playing multiple external swf files one after another.
It’s a very common scenario and I decided to give it a try and managed to do it.
My goal was the create a Flash Movie Slideshow based on external swf files which will play automatically one after another without any additional coding required in any of the external swf files (usually it’s not possible due to unavailability of source fla file anyway). The only requirement is that all of these swf must be AS3. So here’s what I have done.
- Defined an array for the external swf files.
- Loaded the first swf file using Loader class.
- Added preloader for external swf file.
- Added the play/pause/rewind/forward buttons to control the loaded swf
- Once the swf has reached its last frame, removed it and loaded next swf as defined in array
- After playing the last swf, movie stops.
Here’s the complete functional AS3 code and I have added comments on each step.
//define the external swf files in array var swfList:Array = new Array('file1.swf','file2.swf','file3.swf'); //create the container clip var container:MovieClip = new MovieClip(); var currentSWF:MovieClip = new MovieClip(); var swf_no:Number = 0; 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.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler); swfLoader.load(swfURL); var player:swfPlayerBt = new swfPlayerBt(); function ioErrorHandler(event:IOErrorEvent):void { trace(vFile+" file Not Found!"+"\n----------------\nTracing ioErrorHandler: " + event); } function loadProdComplete(e:Event):void { trace(vFile+ " file loaded"); vBox.removeChild(preLoader); vBox.addChild(swfLoader); currentSWF = MovieClip(swfLoader.content); currentSWF.gotoAndPlay(1); player.x = 200; player.y = 350; //attach the swfPlayer buttons vBox.addChild(player); //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); currentSWF.addEventListener(Event.ENTER_FRAME , checkLastFrame); function checkLastFrame(e:Event):void { //trace(vFile+' is at '+currentSWF.currentFrame+ ' frame.' ); if (currentSWF.currentFrame == currentSWF.totalFrames) { currentSWF.stop(); removeSWF(); } } function removeSWF():void { vBox.removeChild(swfLoader); vBox.removeChild(player); currentSWF.removeEventListener(Event.ENTER_FRAME , checkLastFrame); trace(vFile+" removed"); launchNextSWF(); } 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(); } } var preLoader:loader = new loader(); 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.lpc.text ); } } function launchNextSWF() { //launchSWF trace('launch next'); if (swf_no < (swfList.length-1)) { swf_no++; trace('Now playing '+swf_no+' swf'); launchSWF(container, swfList[swf_no]); } else { trace('Show is over, Nothing more to play! :('); } } //put it on stage addChild(container); //start the slideshow with the first swf file from swfList array launchSWF(container, swfList[swf_no]);
You can download the Multiple External SWF Slideshow CS4 fla from here
Hope that helps.
Cheers!
Related posts:
- [AS3] Creating Preloader for External swf in Flash ActionScript3
- [AS3] Load External swf into Main Flash Movie with Play, Pause, Forward, Rewind, Load and Unload Buttons
- [AS3] Load External swf into MovieClip & Play/Pause/Forward/Rewind and Stop it at LastFrame from Main Flash Movie
- External SWF Player with Play, Pause, Forward, Rewind Buttons and Seek Bar
- [AS3] Loading External SWF into MovieClip using Loader Class in Flash ActionScript3
peter
I was wondering if there’s a way to jump to specific file in the array on button click. For example if user clicks a button, second file in the array is loaded, file2.swf.
Ali Qureshi
Hi peter,
yes, it’s definitely possible. I’ll try to add this feature as well and post the fla here.
michael craddock
Hi Ali, great stuff here and thanks for sharing your expertise with us.
My question is same as Peter’s I think on this post: Multiple External SWF Slideshow – Loading & Playing Mupltiple External SWFs in AS3 Flash Movie
I have 4 files that I want to load and have the user select the button to auto play each on click, sans the player buttons (Play, Pause, Forward, Back). Simply click to play swf1, swf2… Can this be done easily?
Ali Qureshi
Hi Michael,
Yes, it’s doable. I have posted already in another article and you can check the available sample fla file as well here in this .
You can easily remove play/pause etc buttons by removing their code in the above example.
You can also easily remove the attachment of player buttons by commenting following code. And add 3 more buttons by copying the existing one to launch other swf files.
michael craddock
Thanks Ali, appreciate the help. Yes removing the player buttons wasn’t a problem. Which other example file are you referring to? This one: External swf file Player with load and unload buttons
Ali Qureshi
Yes, this will help you place buttons to load external swf. You can replicate load button for different swfs which you wanted.
peter
Thank you, I appreciate your help Ali.
Marco
Hola me parece increíble el código, justo ahora estoy tratando de hacerlo pero quiciera hacerlo autoplay para que no tuviera que dale click al boton play en cada archivo…. Es posible!??? Muchas Gracias Ali!
Saludos
Marco
Hola, es posible hacerlo autoplay y evitar el click al botón play???
Saludos y muchas gracias Ali
Ali Qureshi
Hi Marco,
yes, it’s possible to stop the slideshow after each swf has played its entire timeline. I’ll find some time & do the coding & put the file here.
thanks.
Carlos
Good Day,
this code is really useful, but i have a little issue,
when I run the main movie starts playing immediately and not allowing the external swf content stop . Additional features should I use? for example lookroot?
Ali Qureshi
Hi Carlos,
The external swf player only loads and play SWFs one by one, it does not try to manipulate the timeline of loaded swf files. So whatever is defined in the swf file, it plays to the end and then the next swf is loaded.
Ahmed
Hello Mr Ali,
I am unable to control the sound that has been added to the external swf, when i click on the pause button the frame pauses but the sound does not pause. Same with forward, rewind and Play
Is there a solution.
Regards
Ali Qureshi
As far I know, sound cannot be controlled outside the swf where sound is playing. Sound will only stop if it’s residing on the timeline as stream sound. Event sounds are controlled via ActionScript and that you can’t do (IMO) from outside that swf.
Joe
Hi Ali,
Great tutorial, and this so close to what I need to do, but I can’t figure out how to modify it to fit what I need.
One problem I have is that when I use Flash to test the movie, and then simulate the download, the audio plays immediately while the progress bar advances. Once at 100% the whole thing starts over from the beginning (both audio and video).
Also, your code will load one swf at a time, which is great, but I need to load the first swf with the progress of that swf being displayed in the progress bar, then load the others in the background while the first one plays (almost like a streaming effect), so that as soon as the first swf is finished playing the second one will start without any delay or loading.
If you could find some time work this in that would be awesome.
Ali Qureshi
Loading more swf files in the background is easy. The sound playing is mainly the swf issue. Most probably, sound is not pre-loaded before playing but it might also be stopped. I’ll try both of these things and post an update.
Sarah
Hi Ali,
Great tutorial and the example works beautifully. However, I have just one query: is there a way that when the last swf file to be played comes to it’s end, can it stop on the last frame and not be removed from the stage? I’ve tried various functions to try and stop the removeChild function if it’s the last item in the array but no luck. I’m a noob to AS3 so if you can be any help it would be much appreciated.
Many thanks
Ali Qureshi
HI Sarah,
All you have to do is to add this one line before removeSWF(); like this:
So the updated checkLastFrame function would look like this:
The above condition compares the currently playing swf file number in the array with the total number of swf. If it’s the last one, it shows the message otherwise returns true and moves to the next swf.
Cheers!
ali
Hello ali
Great example ali
Ali can we load these .swfs files by using “next” & “previous” buttons.
Ali Qureshi
Check this External swf file Player with load and unload buttons
You can use the same logic for Next and Prev buttons which I have used for Load button on first screen.
Gregg
Ali, this code is perfect for my present need. However I am experiencing the following issue. When I load in my external swf files, the player loads and immediately unloads all files in the span of a second. What am I missing here?
I created 3 simple swf files (all in AS3) to load in order.
It appears to be working, but unable to view each loaded swf content.
Gregg
Ali Qureshi
I think your external swf files consist of just 1 frame. I think as soon as they load, they reach the last frame which is the same frame and they get uploaded. So try with some movie which has its root timeline extended to some frames.
Gregg
Thanks for getting back to me Ali. All of the loaded swf files are AS3 and consist of approximately 20 seconds of animation and audio. The audio continues to play (over and over).
I’ve removed the player controls (commented out) as I don’t need them.
In addition I’m receiving the following errors:
VerifyError: Error #1014: Class flashx.textLayout.container::ContainerController could not be found.
ReferenceError: Error #1065: Variable Font1_7 is not defined.
ReferenceError: Error #1065: Variable MainTimeline is not defined.
Ali Qureshi
To stop audio, add following code at top of your AS3 code:
I’m not sure about your other errors, may be you have not embedded the fonts properly. Try to use device fonts in your text fields.
Vinod
I am not good at Scripting, request you to please help me, I like to place this in AS file please tell me how to do it i’m trying to do but not getting worked
Ali Qureshi
I have provided the sample fla file, download and check the code.
Vinod
Hi im trying with your codes, I have multiple swfs and end of each SWF last frame a library item attach to stage, library item have button for each swf that loads this library itema and swf loads on main swf,, and I have a code that controll the extranl swf timeline its a as file, Could you please help me on this.
thanks
vinod
Ali Qureshi
I’m sorry but I’m too much busy in my own projects. I have provided several fla files in different blog posts for loading external swf files. Just go through them and choose that which suits best to your needs. Of course you will have to adapt code to your needs.