Pages

Thursday 6 February 2014

AS3 Calculating the Time Duration of a SWF file

The below code will calculate the time duration of a SWF file

import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.text.TextField;

// load swf file
var my_Loader:Loader = new Loader();
my_Loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadCompleteF);
login_load.addChild(my_Loader);
addChild(login_load);

var my_url:URLRequest = new URLRequest("swf/screen1.swf");
my_Loader.load(my_url);
login_load.width = 250;
login_load.height = 250;

function loadCompleteF(evt:Event):void
{
addEventListener(Event.ENTER_FRAME, onEnter);
}

function onEnter(evt:Event):void
{
var minutesCap:Number = 0;
var totalCapFrames:Number = MovieClip(my_Loader.content).totalFrames;
var frameCapRate:Number = 24;
var capDuration:Number = Math.round(totalCapFrames / frameCapRate);
var minutesCapRounded:Number = Math.floor(capDuration / 60);
var remainingSecs:Number = (capDuration) - (minutesCapRounded * 60); 
//trace(minutesCapRounded);
//duration.text = minutesCap.toString() + " mins" + " " + capDuration.toString() + " secs";
duration.text = minutesCapRounded.toString() + " mins" + " " + remainingSecs.toString() + " secs"; 
}

Just change the below line of code to your own swf file
var my_url:URLRequest = new URLRequest("swf/screen1.swf");
and you will get the total duration of the SWF file played like 1 mins 38 secs

If you need to find the duration of a Captivate SWF file you would have to import the com classes. Copy the com folder in the same folder as your main SWF file.
Add / change the code as below
Import  the com classes
import com.adobe.captivate.flash.*;
change the below code
var totalCapFrames:Number = MovieClip(my_Loader.content).rdinfoFrameCount;
The other code remains the same.

No comments:

Post a Comment