In AS3 while using NetStream for video playback how do I seek when I use appendBytes

前端 未结 3 1540
萌比男神i
萌比男神i 2020-12-20 03:38

I am trying to use NetStream to play from a byteArray. Please see this for what I am talking about.

I am able to get this to play the video. However now I need to b

3条回答
  •  情话喂你
    2020-12-20 04:01

    Each flv tag has a timestamp and offset. You can find the FLV spec here: http://download.macromedia.com/f4v/video_file_format_spec_v10_1.pdf

    You can't play the video at any position. You must start at the beginning of a tag so you will need to find the tag with a time nearest to the time you want to seek to.

    You're going to want to do something like this:

    private var tags:Array = [];
    private var fileStream:FileStream;  
    private var netStream:NetStream;
    private var seekTime:Number;
    
    public function function readTags(path:String):void
    {
        //open the fileStream for reading
    
        while(fileStream.bytesAvailable > 0)
        {
            //sudo code for reading the tags of an FLV
    
            var tagPosition:int = fileStream.position;
            var tagSize:int = //Read the tag size;
            var timestamp:int = //read the timestamp;
            tags.push({timestamp:timestamp, position:tagPosition}];
            fileStream.position += tagSize;  //goto the next tag
        }
    }
    
    private function findTagPosition(timeInMilliseconds:int):int
    {
        //Search the tags array for the tags[a].timestamp that is nearest to timeInMilliseconds
    
        return tags[indexOfTagNearstToTimeInMilliseconds].position;
    }
    
    public function seek(time:Number):void
    {
        seektime = time;
        netStream.seak(time);
    }
    
    private function onNetStatusEvent(event:NetStatusEvent):void
    {
        if(event.info.code == NetStreamCodes.NETSTREAM_SEEK_NOTIFY)
        {
            fileStream.position = findTagPosition(seekTime * 1000);
            netStream.appendBytesAction(NetStreamAppendBytesAction.RESET_SEEK);
            var bytes:ByteArray = new ByteArray();
            fileStream.readBytes(bytes);
            netStream.appendBytes(bytes);
        }
    }
    

提交回复
热议问题