Recipe 16.4.
Reading Video Duration
Problem
You want
to read the total length (duration) of a video.
Solution
Use an onMetaData( ) callback and read
the duration
metadata value.
Discussion
The NetStream class does not define a property that reports the total
length of a Flash video. However, in most cases, it is possible to
read that value from the FLV file itself. FLV files can contain
metadata, and almost all video encoders include a duration
metadata value, which stores the length of the video in seconds
(which can be fractions of seconds). Assuming the FLV has a
duration metadata value, you can read that value with
ActionScript.
When a NetStream object is loading an FLV
file, it automatically calls an onMetaData( ) callback
method when the metadata has loaded. The callback model differs
from the standard event model used by most of the ActionScript 3.0
APIs. In most cases in which you work with events you add a
listener using addEventListener( ). However, in the
case of metadata events you must define an onMetaData( )
method for an object and then assign that object to the
client property of the NetStream object. The method
is automatically passed an associative array parameter typed as
Object that contains properties and values corresponding to
each of the metadata properties that have been read from the FLV.
The following example uses trace( ) to display the
duration metadata value of a video clip:
var client:Object = new Object( );
client.onMetaData = function(metadata:Object):void {
trace(metadata.duration);
};
videoStream.client = client;
In practice, the preceding example isn't all
that useful. Rather, you're more likely to assign a function
reference to the onMetaData property of the
NetStream object. When the function is called, it is called
with the correct scope. The following example class loads, plays
back a video, and displays the playback time and duration:
package {
import flash.display.TextField;
import flash.media.Video;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.events.NetStatusEvent;
import flash.display.TextFieldAutoSize;
import flash.display.Sprite;
import flash.events.Event;
public class Example extends Sprite {
private var _stream:NetStream;
private var _video:Video;
private var _playbackTime:TextField;
private var _duration:uint;
public function Example( ) {
_video = new Video(160, 120);
_playbackTime = new TextField( );
_playbackTime.autoSize = TextFieldAutoSize.LEFT;
_playbackTime.y = 120;
_playbackTime.text = "test";
_duration = 0;
var connection:NetConnection = new NetConnection( );
connection.connect(null);
_stream = new NetStream(connection);
_stream.play("video.flv");
var client:Object = new Object( );
client.onMetaData = onMetaData;
_stream.client = client;
_video.attachNetStream(_stream);
addChild(_video);
addChild(_playbackTime);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onMetaData(data:Object):void {
_duration = data.duration;
}
private function onEnterFrame(event:Event):void {
if(_duration > 0 && _stream.time > 0) {
_playbackTime.text = Math.round(_stream.time) + " / " +
Math.round(_duration);
}
}
}
}
See Also
Recipe
16.8
|