前に投稿されていた
FMS2でplaylistを自動的に再生する方法の中で、少し触れた
VideoPlayerクラスを使わないで、キューポイントを取得する方法の
試作コードです。
イベント/リスナーの使い方を調べて、何とか動くようになりました。
ここはこうした方がよいとか、こうもできるとかいう議論のたたき台にでもなれば”本望”です。
実際には、FLVPlaybackコンポーネントやVideoPlayerクラスを使えばよいので、
実用的なコードではないですが、このイベント/リスナー形式はほかにも応用できます。
//VideoCuePoint.asファイル
/*指定された.flvファイルを再生し、flvファイルに埋め込まれたキューポイントを
含むイベントオブジェクトを返す*/
import mx.utils.Delegate;
class VideoCuePoint extends MovieClip {
private var _video:Video;
private var _nc:NetConnection;
private var _ns:NetStream;
//
private var dispatchEvent:Function;
public var addEventListener:Function;
public var removeEventListener:Function;
//
public function VideoCuePoint() {
mx.events.EventDispatcher.initialize(this);
//
_nc = new NetConnection();
_nc.connect(null);
}
public function videoPlay(fileName:String):Void {
_ns = new NetStream(_nc);
var sendCue:Function = Delegate.create(this, this.onCuePointCustom);
_ns.onCuePoint = function(info:Object) {
//通常はここで情報をとる
sendCue(info);
};
_video.attachVideo(_ns);
_ns.play(fileName);
}
private function onCuePointCustom(info:Object):Void {
var eventObject:Object = new Object();
eventObject.type = "onCuePoint";
eventObject.name = info.name;
eventObject.time = info.time;
this.dispatchEvent(eventObject);
}
public function addListener(eventName:String, listener:Object):Void {
this.addEventListener(eventName, listener);
}
public function removeListener(eventName:String, listener:Object):Void {
this.addEventListener(eventName, listener);
}
}
//FLAファイル
import mx.controls.TextArea;
var info_txt:TextArea;
//video_mcムービークリップの中には_videoというインスタンス名のVideoオブジェクトを置く。
//リンケージをVideoCuePointに設定。
var video_mc:VideoCuePoint;
//リスナーはメインのタイムライン
var listener = this;
listener.onCuePoint = function(evt:Object) {
displayInfo(evt);
};
function displayInfo(evt:Object):Void {
info_txt.text += "cuePoint : "+evt.name+"\n";
info_txt.text += "時間 : "+evt.time+"\n";
info_txt.text += "イベント : "+evt.type+"\n";
info_txt.vPosition = info_txt.maxVPosition;
}
video_mc.addListener("onCuePoint", listener);
//再生はFMSでなく、Webサーバー上
var flv:String="FLVファイル名.flv"
video_mc.videoPlay(flv);
----------------
永井勝則:
himco.jp :