问题
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 be able to see to a particular second. I am not able to do this. If I go through the bytearray I know I can get the metadata and the keyframes as required and the offset ( in bytes) as to where each keyframe is at. However there is no way to seek to a particular offset. It only supports seek in seconds and that doesn't seem to work on the byteArray.
How do I go about this ?
回答1:
I got I worked:
// onmetadata function get all timestamp and corresponding fileposition..
function onMetaData(infoObject: Object): void {
for (var propName: String in infoObject) {
if (propName == "keyframes") {
var kfObject: Object = infoObject[propName];
var timeArr: Array = kfObject["times"];
var byteArr: Array = kfObject["filepositions"];
for (var i: int = 0;i < timeArr.length;i++) {
var tagPos: int = byteArr[i]; //Read the tag size;
var timestamp: Number = timeArr[i]; //read the timestamp;
tags.push({
timestamp: timestamp,
tagPos: tagPos
});
}
}
// onseek click get approximate timestamp and its fileposition
protected
function seek_click(seektime: Number): void {
var cTime: Number = 0;
var pTime: Number = 0;
for (var i: int = 1;i < tags.length;i++) {
cTime = tags[i].timestamp;
pTime = tags[i - 1].timestamp;
if (pTime < seektime) {
if (seektime < cTime) {
seekPos = tags[i - 1].tagPos;
stream.seek(pTime);
break;
}
}
}
}
/// append bytes on seekposition
private
function netStatusHandler(event: NetStatusEvent): void {
switch (event.info.code) {
case "NetStream.Seek.Notify":
stream.appendBytesAction(NetStreamAppendBytesAction.RESET_SEEK);
totalByteArray.position = seekPos;
var bytes: ByteArray = new ByteArray();
totalByteArray.readBytes(bytes);
stream.appendBytes(bytes);
stream.resume();
break;
}
}
回答2:
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.
Your 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);
}
}
回答3:
Try my code posted here for an example of how seeking is done without needing "keyframes" to exist in FLV metadata etc. There is also a working demo available to test (use FLV with H264 and AAC/MP3 audio).
That code was partly to answer this question (if you need additional info/context)
来源:https://stackoverflow.com/questions/3825103/in-as3-while-using-netstream-for-video-playback-how-do-i-seek-when-i-use-appendb