Sometime back I had posted several tutorials explaining how to load external swf file into main flash movie and then control the timline of loaded swf from the main flash movie. I had been getting requests on daily basis for the questions on how to load and unload the external swf file on user click. So here’s the updated code and the source fla for you to experiment.
This fla file will add a launch button on the stage and on clicking, it will load the external swf file on stage with the player Play/Rewind/Forward/Stop buttons to control the timeline of loaded swf. On loading, an unload button will also be added to stage which can be used to unload the loaded swf and upon unloading, the load button back on stage and so on..
function launchSWF(vBox, vFile):void{ 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); function checkLastFrame(e:Event):void { if (currentSWF.currentFrame == currentSWF.totalFrames) { currentSWF.stop(); // trace("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(); } } 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 ); } } //create the load button from library, I have already exported it for actionscript 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); //create the unload button from library, I have already exported it for actionscript 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(); //remove loaded swf file removeChild(container);//remove loaded swf container //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-movie.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-load-unload-buttons here. Just provide the swf file url to load and you are done!
Hope that helps.
Cheers!
how to add seek bar to control the external swf, i mean i will have a play/pause/stop button and a slider to work with loaded swf
thaks
Hi sututuyet,
It’s a good question and seek bar can be added using the Flash Slider component. I have made a new post here explaining how Slider component can be used as seek bar to manage externally loaded swf file timeline.