Getting video length in AS3

To get the length of a loaded FLV, you need to have an object listening to the meta data stream. Here’s how:

var vidClient:Object = new Object();
vidClient.onMetaData = onMetaData;
ns.client = vidClient; //ns is the netstream object

function onMetaData(metadata:Object):void {
lengthOfVideo = int(metadata.duration * 1000)+1;
trace("Length of flv from metadata = " + lengthOfVideo);

 

vidClient.onMetaData = null;
vidClient = null;
}

If you’re wondering how to actually load and play the flv file:

var nc : NetConnection;
var ns : NetStream;
var vid : Video;
var path : String = "test.flv";

nc = new NetConnection();
nc.connect(null);
ns = new NetStream(nc);
addChild(vid);

//optional events to handle status and error
ns.addEventListener(NetStatusEvent.NET_STATUS, statusHandler);
ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);

vid.attachNetStream(ns);
ns.play(path);

Leave a Reply

Your email address will not be published. Required fields are marked *