Detecting stream lengthAbout detecting stream lengthCall the server-side Stream.length() method to get the length, in seconds, of an audio or video stream. The length is measured by Flash 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 streamThis example uses Server-Side ActionScript to get the length of a stream. Note: Use the StreamLength sample, main.asc (Server-Side
ActionScript) and StreamLength.as (ActionScript 3.0). To run the
sample, see the general instructions in Deploy an application.
Write the server-side codeA 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 classFrom 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.
Write the client event handler class As usual with playing
a stream, 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");
}
}
|
|