[AS3] Load External swf into MovieClip & Play/Pause/Forward/Rewind and Stop it at LastFrame from Main Flash Movie

Tags:

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!

Related posts:

  1. [AS3] Load External swf into Main Flash Movie with Play, Pause, Forward, Rewind, Load and Unload Buttons
  2. External SWF Player with Play, Pause, Forward, Rewind Buttons and Seek Bar
  3. Multiple External SWF Slideshow – Loading & Playing Mupltiple External SWFs in AS3 Flash Movie
  4. [AS3] Loading External SWF into MovieClip using Loader Class in Flash ActionScript3
  5. [AS3] Creating Preloader for External swf in Flash ActionScript3