Using Communication Objects > NetStream object > Getting the stream time length in ActionScript

 

Getting the stream time length in ActionScript

If you are buffering your streams, you can use the NetStream.bufferLength property to get the number of seconds currently in the buffer. Sometimes, however, you may want to get the total length of a stream; in this case, the Flash Player doesn't know the length of the stream, but the server does. The server has a Stream.length property that the client can request through a message to the server.

You could write client-side ActionScript such as the following to request the stream length:

function getInfo()
{
 nc.call("sendInfo", new MyResultSetName(), myStream);
}
function MyResultSetName()
{
 this.onResult = function(retVal)
 {
	_root.streamlength = retVal;
 };
 this.onStatus = function(info)
 {
	trace("Level: " + info.level + "   Code: " +  info.code);
  // process error object
 };
}

Then, in the corresponding server-side ActionScript, you would write the following in the main.asc file:

application.onAppStart = function()
{
	trace("::: Application has started :::");
}

application.onConnect = function(client)
{
	application.acceptConnection(client);

	// Add methods
	client.prototype.sendInfo = function(name) {
		var slen = Stream.length(name);
		trace("slen: " + slen);
		return slen;
	};
}