用NetStream的appendBytes播放FLV

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);
    }
}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。