我有一个加载和播放flv视频文件的类。我的问题是,如何在主代码中获取flv的持续时间? .. 这是我的视频课程:
package src {
import flash.display.Sprite;
import flash.media.Video;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.events.Event;
import flash.events.MouseEvent;
public class vplayer extends Sprite{
public var vid:Video = new Video(1920,1080);
private var nc:NetConnection = new NetConnection();
public var ns:NetStream;
public var listener:Object = new Object();
public function vplayer():void{
addChild(vid);
nc.connect(null);
ns = new NetStream(nc);
vid.attachNetStream(ns);
listener.onMetaData = metaDataHandler;
ns.client = listener;
//customClient.onCuePoint = cuePointHandler;
}
public var time00:Number = 0;
public function playVideo00():void{
ns.play("image00/mov00.flv");
}
public function stopVideo00():void{
ns.close();
}
/*public function cuePointHandler(infoObject:Object):void {
trace("cuePoint");
}*/
public function metaDataHandler(infoObject:Object):void {
trace (" Time: " + infoObject["duration"]);
}
}
}
..在我的主要课程中我有:
Veed = new vplayer();
addChild(Veed);
Veed.playVideo00();
运行程序后,我在输出窗口中有“时间:6.76”并且它播放flv很好,这是正确的但我想要像
var myTD:int = Veed.getDuration();
在我的主要课程中获得持续时间。这似乎很容易,但我还不能完成它!有什么帮助吗?
答案 0 :(得分:1)
在顶部添加私有变量:
private var _duration:Number = 0;
像这样更新:
public function metaDataHandler(infoObject:Object):void
{
_duration = infoObject["duration"];
}
现在创建一个持续时间的getter。
public function get duration():Number
{
return _duration;
}
现在你可以做:
trace(Veed.duration);
元数据将在请求后几毫秒被检索,这意味着如果您在获取元数据后立即尝试获取持续时间,则它将不会被加载。试试这个例子:
addEventListener(Event.ENTER_FRAME, _wait);
function _wait(e:Event):void
{
if(Veed.duration != 0)
{
trace(Veed.duration);
removeEventListener(Event.ENTER_FRAME, _wait);
}
}