Search code examples
actionscript-3byteflvdecode

Decode flv in bytes in AS3


I am streaming flv file trough vlc media player running as http streaming server. So I am able to get the bytes but how to decode them?

Shoud I take float from the URLStream with readFloat() or plain bytes with readBytes()?


Solution

  • So long as only flv's are streaming, you will want to use the NetStream.appendBytesAction and NetStream.appendBytes for http streaming flv playback. Checkout the following blog post at ByteArray.org and also the quick example below:

    AppendBytes


    Playback Initialization:

    var video:Video = new Video(width, height);
    var video_nc:NetConnection = new NetConnection();
    var video_ns:NetStream = new NetStream();
    
    video_nc.connect(null);
    video_ns.play(null);
    video_ns.appendBytesAction(NetStreamAppendBytesAction.RESET_BEGIN);
    
    video.attachNetStream(video_ns);
    

    ProgressEvent.PROGRESS Handler:

    video_ns.appendBytes(bytesAvailable);
    

    This is essentially the jist of it, bytesAvailable will represent the read bytes from the event data buffer. A full example is listed below:

    package
    {
    
    import flash.display.Sprite;
    import flash.events.NetStatusEvent;
    import flash.events.ProgressEvent;
    import flash.media.Video;
    import flash.net.NetConnection;
    import flash.net.NetStream;
    import flash.net.NetStreamAppendBytesAction;
    import flash.net.URLRequest;
    import flash.net.URLStream;
    import flash.utils.ByteArray;
    
    [SWF(width="1280", height="720")]
    public class NetStreamAppendBytes extends Sprite
    {
    
        var video:Video;
        var video_nc:NetConnection;
        var video_ns:NetStream;
    
        var video_stream:URLStream; 
    
        public function NetStreamAppendBytes()
        {
            super();
    
            video_nc = new NetConnection();
            video_nc.connect(null);
    
            video_ns = new NetStream(video_nc);
            video_ns.client = this;
            video_ns.addEventListener(NetStatusEvent.NET_STATUS, ns_statusHandler);
    
            video = new Video(1280, 720);
            video.attachNetStream(video_ns);
            video.smoothing = true;
    
            video_ns.play(null);
            video_ns.appendBytesAction(NetStreamAppendBytesAction.RESET_BEGIN);
    
            video_stream = new URLStream();
            video_stream.addEventListener(ProgressEvent.PROGRESS, videoStream_progressHandler);
    
            video_stream.load(new URLRequest("path_to_flv"));
    
            addChild(video);
        }
    
        private function ns_statusHandler(event:NetStatusEvent):void
        {
            trace(event.info.code);
        }
    
        private function videoStream_progressHandler(event:ProgressEvent):void
        {
            var bytes:ByteArray = new ByteArray();
    
            video_stream.readBytes(bytes);
            video_ns.appendBytes(bytes);
        }
    
    }
    
    }
    

    Best of luck!