About detecting stream length

Call the server-side Stream.length() method to get the length, in seconds, of an audio or video stream. The length is measured by Adobe Media Server and differs from the duration that onMetaData returns, which is set by a user or a tool.

Pass the stream name to the Stream.length() method. You can pass a virtual stream name or a stream name in a URI relative to the application instance.

For example, the following code gets the length of a stream located in the streams/_definst_ folder of an application:

// for an FLV file 
 length = Stream.length("parade"); 
// for an MP3 file 
 length = Stream.length("mp3:parade.mp3"); 
// for an MP4 file 
 length = Stream.length("mp4:parade.mp4");

Get the length of a stream

This example uses Server-Side ActionScript to get the length of a stream.


Use the StreamLength sample, main.asc (Server-Side ActionScript) and StreamLength.as (ActionScript 3.0). To run the sample, see Deploy an application.

Write the server-side code

A client might need to retrieve the length of a stream stored on the server, for example, if a Flash presentation displays the length of a video to let the user decide whether to play it.

To do this, define a method in server-side code that calls Stream.length(), and then have the client call it using NetConnection.call().

  • In main.asc, define a function on the client object that calls Stream.length(). Do this within the onConnect handler:

     application.onConnect = function( client ) { 
         client.getStreamLength = function( streamName ) { 
             trace("length is " + Stream.length( streamName )); 
             return Stream.length( streamName ); 
         application.acceptConnection( client ); 

Write the main client class

From the main client class, you call getStreamLength() in the server-side code. You need to create a Responder object to hold the response:

 var responder:Responder = new Responder(onResult);

This line specifies that the onResult() function will handle the result. You also need to write onResult(), as shown in the following steps.

  1. In your client code, create a package, import classes, and define variables as usual:

     package { 
         import flash.display.Sprite; 
         import flash.net.NetConnection; 
         import flash.events.NetStatusEvent; 
         import flash.net.NetStream; 
         import flash.net.Responder; 
         import flash.media.Video; 
  2. Create a new class, StreamLength:

         public class StreamLength extends Sprite 
             var nc:NetConnection; 
             var stream:NetStream; 
             var video:Video; 
             var responder:Responder; 
  3. In the constructor for the StreamLength class, call NetConnection.connect() to connect to the server:

     public function StreamLength() 
         nc = new NetConnection(); 
         nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler); 
  4. Add a netStatus event handler to handle a successful connection, rejected connection, and failed connection:

     private function netStatusHandler(event:NetStatusEvent):void 
         trace("connected is: " + nc.connected ); 
         trace("event.info.level: " + event.info.level); 
         trace("event.info.code: " + event.info.code); 
         switch (event.info.code) 
             case "NetConnection.Connect.Success": 
              trace("Congratulations! you're connected"); 
             case "NetConnection.Connect.Rejected": 
             case "NetConnection.Connect.Failed": 
              trace ("Oops! the connection was rejected"); 
  5. Write a function to play the stream when a successful connection is made. In it, create a Responder object that handles its response in a function named onResult(). Then call NetConnection.call(), specifying getStreamLength as the function to call on the server, the Responder object, and the name of the stream:

     // play a recorded stream on the server 
     private function connectStream(nc:NetConnection):void { 
         stream = new NetStream(nc); 
         stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler); 
         stream.client = new CustomClient(); 
         responder = new Responder(onResult); 
         nc.call("getStreamLength", responder, "bikes" ); 
  6. Write the onResult() function to handle the stream length returned by getStreamLength() on the server:

     private function onResult(result:Object):void { 
                    trace("The stream length is " + result + " seconds"); 
                 output.text = "The stream length is " + result + " seconds"; 

Write the client event handler class

  • Write a separate class to handle the onMetaData and onPlayStatus events:

     class CustomClient { 
         public function onMetaData(info:Object):void { 
             trace("metadata: duration=" + info.duration + " width=" + info.width +  
             " height=" + info.height + " framerate=" + info.framerate); 
         public function onPlayStatus(info:Object):void { 
             trace("handling playstatus here"); 

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License  Twitter™ and Facebook posts are not covered under the terms of Creative Commons.

Legal Notices   |   Online Privacy Policy