+展开-ActionScript
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.NetStatusEvent;
import flash.media.Video;
import flash.net.NetConnection;
import flash.net.NetStream;
import mx.core.UIComponent;
public class Scrubber extends UIComponent
{
private var playhead:Sprite;
private var timeline:Sprite;
private var ns:NetStream;
private var nc:NetConnection;
private var obj:Object = {};
private var length:int;
private var vid:Video;
public function Scrubber () {
super();
playhead = new Sprite();
addChild(playhead);
playhead.graphics.beginFill(0x0000ff, 1);
playhead.graphics.drawCircle(0, 0, 5);
playhead.graphics.endFill();
playhead.addEventListener(MouseEvent.MOUSE_DOWN,
startSeek);
timeline = new Sprite();
timeline.graphics.beginFill(0xcccccc, 1);
timeline.graphics.drawRect(0, 0, 200, 10);
timeline.graphics.endFill();
addChild(timeline);
timeline.addChild(playhead);
playhead.y = 4;
vid = new Video();
addChild(vid);
vid.y = 100;
nc = new NetConnection();
nc.addEventListener(NetStatusEvent.NET_STATUS,
netStatus);
nc.connect(null);
}
private function netStatus(event:NetStatusEvent):void {
obj.onMetaData = onMetaData;
ns = new NetStream(nc);
ns.client = obj;
vid.attachNetStream(ns);
ns.play("http://localhost:3001/test.flv");
}
private function onMetaData(obj:Object):void {
length = obj.duration;
trace(length);
}
private function startSeek(mouseEvent:MouseEvent):void {
playhead.startDrag(false, timeline.getBounds(this));
addEventListener(MouseEvent.MOUSE_MOVE, seek);
playhead.addEventListener(MouseEvent.ROLL_OUT, endSeek);
playhead.addEventListener(MouseEvent.MOUSE_UP, endSeek);
}
private function seek(mouseEvent:MouseEvent):void {
ns.seek((playhead.x/timeline.width) * length);
}
private function endSeek(mouseEvent:MouseEvent):void {
removeEventListener(MouseEvent.MOUSE_MOVE, seek);
playhead.stopDrag();
}