在AS3中以字节为单位解码flv

时间:2011-11-27 12:43:44

标签: actionscript-3 byte flv decode

我正在通过运行为http流媒体服务器的vlc媒体播放器传输flv文件。所以我能够得到字节,但如何解码它们?

Shoud我从带有readFloat()的URLStream或带有readBytes()的普通字节中取浮动?

1 个答案:

答案 0 :(得分:1)

只要flv是流式传输,你就会想要使用NetStream.appendBytesAction和NetStream.appendBytes进行http流式传输。查看ByteArray.org上的以下博客文章以及下面的快速示例:

AppendBytes


播放初始化:

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处理程序:

video_ns.appendBytes(bytesAvailable);

这基本上就是它的jist,bytesAvailable将表示来自事件数据缓冲区的读取字节。下面列出了一个完整的例子:

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

}

}

祝你好运!