This is last of the series of posts I have made on loading external swf movies into Main Flash movie and controlling its timeline. In this post, I’ll add the last missing feature in the player which was seek bar. In the previous Post, I added Play, Pause, Forward, Rewind Buttons to control and manage the timeline of external loaded swf file and this addition will complete the player.
If you want to see the explanation for the whole code, you can view it here.
In this post, I have used Flash Slider component to make a seek bar. This seek bar shows the total number of frames of loaded swf file and then plays through it and update the slider position with the current frame position. It’s done on EnterFrame event so this slider location is checked on every frame and updated until the lastFrame of the swf file is reached.
Here’s the complete post as AS3 code. Make sure you drag the Slider component onto the stage from the Components.
[actionscript3] function launchSWF(vBox, vFile):void{ //vBox.addChild(swfLoader); var swfURL:URLRequest = new URLRequest(vFile); swfLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler); swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadProdComplete); swfLoader.load(swfURL); var player:swfPlayerBt = new swfPlayerBt(); function loadProdComplete(e:Event):void { trace("swf 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); import fl.controls.Slider; //add slider component var aSlider:Slider = new Slider(); //Define the width and location on stage aSlider.width = 200; aSlider.move(160, 330); //add it to the container clip vBox.addChild(aSlider); trace(currentSWF.totalFrames); aSlider.maximum = currentSWF.totalFrames; aSlider.liveDragging=true; aSlider.addEventListener(Event.CHANGE,swfHandler); function swfHandler(e:Event){ trace("aSlider.value: "+aSlider.value); currentSWF.gotoAndStop(aSlider.value); } function checkLastFrame(e:Event):void { if (currentSWF.currentFrame == currentSWF.totalFrames) { currentSWF.stop(); // trace("DONE"); }else aSlider.value =currentSWF.currentFrame; } 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.bar.scaleX ); } } var loadButton:btLoad = new btLoad(); //place load button in the cetner of stage loadButton.x = (stage.stageWidth / 2) - (loadButton.width / 2); loadButton.y = (stage.stageHeight / 2) - (loadButton.height / 2); var unloadButton:btUnload = new btUnload(); //place unload button in the cetner of stage unloadButton.x = (stage.stageWidth / 2) - (unloadButton.width / 2); unloadButton.y = 15; loadButton.addEventListener(MouseEvent.CLICK, button_load); function button_load(e:Event):void{ launchSWF(container, swfFile); //put it on stage addChild(container); //remove the load button, it won't work and needed now removeChild(loadButton); //add the unload button now addChild(unloadButton); } unloadButton.addEventListener(MouseEvent.CLICK, button_unload); function button_unload(e:Event):void{ swfLoader.unloadAndStop(); removeChild(container);//remove loaded swf container //gotoAndStop(10);// send to main timeline frame 10 //remove the unload button, it won't work and needed now removeChild(unloadButton); //add the uload button now addChild(loadButton); } var container:MovieClip = new MovieClip(); var swfFile:String = 'external-file.swf'; var currentSWF:MovieClip = new MovieClip(); var swfLoader:Loader = new Loader(); //place the load button on stage addChild(loadButton);
I have put together all this into this file. You can Download CS4 fla source file for loading-external-swf-with-preloader-play-stop-seekbar here. Just provide the swf file url to load and you are done!
Hope that helps.
Cheers!
my seekbar working on flv video player but not working in my flash non video animation play pause stop revine all buttons are working but seekbar only problem in as3 if anyone know the info pls tel me
seekbar will work on non video animation …? it’s posible….?
Where do you edit the url? And is it possible to make it auto load the external swf automactly? I downloaded a flv movie and converted it into .swf but i wanted to find a way to put the play/stop buttons and maybe a slider if possible using flash. This tool can accomplish that right? Thanks
You can change the url of the swf in the actions panel.
Yes, it does add the stop play button inside Flash.
After i while i released i had to change the so it would load.
var swfFile:String = ‘external-file.swf’;
It loads perfectly when i click to load and unload. But the buttons don’t seem to work at all :S Was it because the .swf was converted from .flv ?
It may well be the reason. Check and try that your external file content is spread on root timeline and not in a movie clip.
I think that’s the problem because i can’t get it to convert back to .fla so this won’t work at all right? Is there any way to make it like your example? I need to add some loader (with play button,stop etc) that controls the 2nd .swf
You don’t really need to convert flv back to swf. You can easily control flv timeline with buttons using video component. I developed this swf for those files which were swf and there was no way to manage external swf timeline. You can do that with externally loaded flv files.
Check this post where I have shown how you can manage that.
forget to mention *
The index.html at location
(http://www.bookmyownevents.com) is having a object tag pointing to swf file under the uploads directory.
This index.html file is not able to load the external swf file correctly.
Request to have a look, and provide any suggestions.
Thanks in advance.
It’s basically resources path issue. When you are loading any item, it must be referenced from the url where it’s embedded i.e. all links to other swfs in the embedded swf must include ‘uploads/’.
Another foolproof way is to use absoulute path starting with http://
Thanks a lot for your help Ali
God Bless you
🙂
Thanks for the wonderful work Ali,
But I am getting following error,
TypeError: Error #1034: Type Coercion failed: cannot convert flash.display::AVM1Movie@443ff31 to flash.display.MovieClip.
at Function/external_fla:MainTimeline/launchSWF/external_fla:loadProdComplete()[external_fla.MainTimeline::frame1:21]
Could please help me in what I am doing wrong here ?
Thanks.
Please make sure you are loading AS3 movie. Type Coercion errors appears usually when the AS2 movie is loaded into AS3.
Thanks a lot.
yeah, I was trying to load AS2 clip. I converted to AS3 and now it works fine.
I have one more problem
I have loaded all files (external swf file, main control swf file, and html file) in same directory “http://www.bookmyownevents.com/uploads/”
and everything is working fine.
But when I upload the html file having the object tag with src pointing to the main swf file (http://www.bookmyownevents.com/uploads/swfPlayer2.swf)
Then the swf file is not played at all.
Please suggest any idea to rectify this.
Brilliant, thanks for this, just what i was after.
I did notice that if you load and unload, the previous point on the slider bar is still there.
I moved:
import fl.controls.Slider;
var aSlider:Slider = new Slider();
out of the loadProdComplete function and to the top of the page, which seems to have done the trick.
I’m guessing it was adding a new slider each time a swf was loaded without removing the old one.
(i’m fairly new to AS3 so this may be incorrect!)
Please…
didn’t work on Flash CS5
seeker, play, and pause buttons didn’t work when clicked
Output:
swf file loaded
TypeError: Error #1034: Type Coercion failed: cannot convert flash.display::AVM1Movie@25938161 to flash.display.MovieClip.
at Function/()
Create a new AS3 flash movie and load its swf as test.
This is crap. It doesn’t work properly. It plays multiple instances of my streaming sound and shows none of the visuals. Crappity crap crap
The slideshow only shows and attempts to load and play the external swf files. Your external swfs must be coded correctly with the visuals and audio streamed on the root timeline. If you have placed everything in the nested clips and your swf consists of just 1 frame then this is not for you. Go ahead an make one for yourself. 🙂
hi ali yeah i tried this,
its working perfect on local pc but when i upload same problem.
—
i changed it.
loaded swf file
stop() 1st frame
and in ur code. i updated 1 -> 2 frame, its working perfect now.
currentSWF = MovieClip(swfLoader.content);
currentSWF.gotoAndPlay(2);
thank you so much for ur help ali
stay blessed.
I’m glad it worked.
thanks ali, i will try this
hello ali
great work ali, am using this facing bit problem
external loaded swf start playing its sound , it should load 100% first, then it should start playing sound.
sound is synchronized in external loaded file, so its should play when its fully load 100%.
please help.. thanks
If your sound is streaming and placed on separate layer, then start it from 2nd frame. And if you are using AS3 code to attach sound dynamically from library, then take that AS3 code inside where swf has already loaded. It all depends upon the inclusion method of sound in your swf.
yes sound is streaming and i start it from 2nd frame , but still its playing before loading whole swf file [100%].
—
i also tried to load from mp3 file from library by using AS3, that is working fine but i need streaming sound is there any way to deal that loaded sound to play like a stream sound on timeline?
Try this, stop all sound son frame 1 using following code, and once the swf starts playing, it will play the streaming sound when reaches to frame 2 and onward.
Thank you 🙂