public class MiniStream extends Sprite { private var _buffer:ByteArray = new ByteArray(); private var _ns:NetStream; private var _nc:NetConnection; private var _video:Video; private var _tc:Number = 0; private var _ustream:URLStream; private var _elapsed_bytes:uint = 0; public function MiniStream(onVideo:Function) { _video = new Video(400,300); addChild(_video); _ustream = new URLStream(); _ustream.addEventListener(IOErrorEvent.IO_ERROR, onErr); _ustream.addEventListener(ProgressEvent.PROGRESS, onProgress); } public function play():void { _nc = new NetConnection(); _nc.connect(null); if(_ns) { _ns.removeEventListener(NetStatusEvent.NET_STATUS, onStatus); } _ns = new NetStream(_nc); _ns.addEventListener(NetStatusEvent.NET_STATUS, onStatus); _ns.client = {}; _ns.bufferTime = 3; _video.attachNetStream(_ns); _ns.play(null); _ns.appendBytesAction(NetStreamAppendBytesAction.RESET_BEGIN); _ns.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler); _ustream.load("http://example.com/h264vid.flv"); } private function onStatus(e:NetStatusEvent):void { trace(e.info.code); } private function onProgress(e:ProgressEvent):void { //stores in our BA buffer _ustream.readBytes(_buffer,0,_ustream.bytesAvailable); _buffer.position = 0; if(_buffer.bytesAvailable > 0) { _ns.appendBytes(_buffer); _elapsed_bytes += _buffer.length; _buffer.clear(); } } private function netStatusHandler(evt:NetStatusEvent):void { if (evt.info.code == "NetStream.Play.Stop") { _ns.appendBytesAction(NetStreamAppendBytesAction.END_SEQUENCE); } } private function onErr(e:IOErrorEvent):void { //trace("ERROR", e.text); } }
时间: 2024-10-11 07:23:37