play flv using netStream appendBytes

2.5k Views Asked by At

I know that there are many ways to play an FLV file but considering my project requirements, I need to play the flv using URLStream and NetStream

here's the complete sample code that I'm doing my tests on:

package 
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.NetStatusEvent;
    import flash.events.ProgressEvent;
    import flash.utils.ByteArray;
    import flash.net.URLRequest;
    import flash.net.URLLoader;
    import flash.net.URLLoaderDataFormat;
    import flash.net.NetStreamAppendBytesAction;
    import flash.net.NetConnection;
    import flash.net.NetStream;
    import flash.media.Video;
    import flash.net.URLStream;

    /**
     * ...
     * @author Hadi Tavakoli
     */
    public class Main extends Sprite 
    {
        private var netConnection:NetConnection;
        private var netStream:NetStream;
        private var ul:URLStream;
        private var video:Video;
        private var bytes:ByteArray = new ByteArray();
        private var _isSeek:Boolean = false;

        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }

        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            // entry point

            video = new Video();
            addChild(video);

            netConnection = new NetConnection();
            netConnection.addEventListener(NetStatusEvent.NET_STATUS, netConnectionStatusHandler);
            netConnection.connect(null);
        }

        private function netConnectionStatusHandler(ev:NetStatusEvent):void
        {
            switch(ev.info.code)
            {
                case 'NetConnection.Connect.Success':

                    ul = new URLStream();
                    ul.addEventListener(ProgressEvent.PROGRESS, onProgress);
                    ul.load(new URLRequest('01.flv'));

                break;
            }
        }

        private function onProgress(e:ProgressEvent):void
        {
            ul.readBytes(bytes, bytes.length);

            if (!netStream) 
            {
                netStream = new NetStream(netConnection);
                netStream.client = { };
                video.attachNetStream(netStream);
                netStream.play(null);
                trace("BEGIN")
                netStream.appendBytesAction(NetStreamAppendBytesAction.RESET_BEGIN);
            }
            else
            {
                if (!_isSeek)
                {
                    trace("SEEK")
                netStream.appendBytesAction(NetStreamAppendBytesAction.RESET_SEEK);
                    _isSeek = true;
                }
            }

            if (bytes.length == e.bytesTotal)
            {
                trace("END")
            netStream.appendBytesAction(NetStreamAppendBytesAction.END_SEQUENCE);
            }

            netStream.appendBytes(bytes);
            trace("-")
        }
    }
}

I'm not sure if I am using "appendBytes" method correctly? the video is shown but only a very few first frames will play and then the video stops!

in my eyes it seems all ok! do you have any advice on where my problem is?

2

There are 2 best solutions below

2
On

in "ul.readBytes(bytes, bytes.length);" line, there is a bug i guess. It's never worked for me also. It always return full length (from 0 to the available bytes). So It have a huge memory leak. But if you are using flash player 11.4 or later, you can change it like this.

ul.position = bytes.length;
ul.readBytes(bytes);
0
On

I don't think you need the if (!_isSeek) block. It looks like you are pushing the bytes as you receive them in a sequential order and so there's never a seek. It looks like it will push the first set of bytes and then append a seek action and append the rest of the bytes. Try just removing that block and see if it works.

Otherwise I think it's ok.