我正在尝试使用NetStream从字节数组播放。请看 这个 对于我所说的。
我能够让这个来播放视频。然而,现在我需要能够看到一个特定的第二。我无法做到这一点。如果我通过字节数组,我知道我可以得到元数据和所需的关键帧以及每个关键帧的偏移量(以字节为单位)。然而,没有办法寻找到一个特定的偏移量。它只支持以秒为单位的搜索,但似乎在字节数组上不起作用。
我该如何解决这个问题?
我得到了我的工作。
// 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;
}
}
每个flv标签都有一个时间戳和偏移量,你可以在这里找到flv的规范。http:/download.macromedia.comf4vvideo_file_format_spec_v10_1.pdf。
你不能在任何位置播放视频。你必须从一个标签的开头开始,所以你需要找到一个时间最接近你想寻求的时间的标签。
你要想做这样的事情。
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);
}
}