Multiple External SWF Slideshow – Loading & Playing Mupltiple External SWFs in AS3 Flash Movie

Tags:

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:

  1. [AS3] Creating Preloader for External swf in Flash ActionScript3
  2. [AS3] Load External swf into Main Flash Movie with Play, Pause, Forward, Rewind, Load and Unload Buttons
  3. [AS3] Load External swf into MovieClip & Play/Pause/Forward/Rewind and Stop it at LastFrame from Main Flash Movie
  4. External SWF Player with Play, Pause, Forward, Rewind Buttons and Seek Bar
  5. [AS3] Loading External SWF into MovieClip using Loader Class in Flash ActionScript3