as3 Netstream appendBytes and seeking
I have a simple flv player which connects to a CDN which can take header byte range requests if a user wants to skip through a flv without the need for the flv to be 100% loaded.
I am using a third party http librabry to add the range header to the request.
My code plays the flv but when I try to skip to 30 seconds into the video it just freezes. I beleive the cdn does return the file as i can trace through the bytes and the header has the content-range repsonse (below).
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
var flvBT:String = "addresstoflv.flv";
client = new HttpClient();
uri = new URI(flvBT);
nc = new NetConnection();
nc.connect(null);
myVideo = new Video();
myVideo.smoothing = true;
myVideo.x = 10;
myVideo.y = 10;
myVideo.width=640;
myVideo.height=360;
addChild(myVideo);
stage.addEventListener(MouseEvent.CLICK, onClickListener);
seeked = false;
setupNetStream();
}
private function setupNetStream():void {
request = new Get();
if (seeked) {
request.addHeader("Range", "bytes="+bytes+"-");
}
client.listener.onRequest = function(e:HttpRequestEvent):void{
trace(e.request);
}
ns = new NetStream(nc);
ns.client = {};
ns.client.onMetaData = metaDataHandler;
myVideo.attachNetStream(ns);
ns.play(null);
ns.appendBytesAction(NetStreamAppendBytesAction.RESET_BEGIN);
client.addEventListener(HttpRequestEvent.COMPLETE, requestComplete);
client.addEventListener(HttpDataEvent.DATA, onDataListener);
client.request(uri, request);
}
private function onClickListener(e:MouseEvent):void {
trace("clicked")
seeked = true;
ns.seek(30);
bytes = metaData.getPdSeekValue(30);
ns.appendBytesAction(NetStreamAppendBytesAction.RESET_SEEK);
setupNetStream();
}
private function requestComplete(e:HttpRequestEvent):void {
trace("request complete");
}
private function metaDataHandler(item:Object):void {
metaData = new StreamMetaData(item, false);
trace("metaData");
// Resize video instance.
myVideo.width = item.width;
myVideo.height = item.height;
// Center video instance on Stage.
myVideo.x = (stage.stageWidth - myVideo.width) / 2;
myVideo.y = (stage.stageHeight - myVideo.height) / 2;
}
private function onDataListener(e:HttpDataEvent):void {
ns.appendBytes(e.bytes);
}
开发者_开发问答
StreamMetaData class
...
private function getPdSeekValue(approxTime:Number, times:Array, lower:int, upper:int):int {
if (upper - lower <= 1)
return lower;
var mid:int = Math.floor((lower + upper) / 2);
if (approxTime <= times[mid]){
return getPdSeekValue(approxTime, times, lower, mid);
} else {
return getPdSeekValue(approxTime, times, mid, upper);
}
}
....
First Header request and response
Header:
GET flvfile.flv HTTP/1.1
Host: cdnserver.net
Connection: close
method: GET, header: Connection: close, body: null
request complete
Response header:
version: 1.1, code: 200, message: OK
header:
Connection: Close
Accept-Ranges: bytes
ETag: "82639398e26cc1:0"
Last-Modified: Thu, 09 Jun 2011 10:15:57 GMT
Content-Type: video/x-flv
Content-Length: 8611520
Date: Tue, 13 Sep 2011 11:19:58 GMT
Server: Cisco-CDS
Response: 200
Seek header request and resonse
Header:
GET flvfile.flv HTTP/1.1
Host: cdnserver.net
Connection: close
Range: bytes=1206339-
method: GET, header: Connection: close
Range: bytes=1206339-, body: null
request complete
Response header:
version: 1.1, code: 206, message: Partial Content
header:
Connection: Close
Accept-Ranges: bytes
Content-Range: bytes 1206339-8611519/8611520
ETag: "82639398e26cc1:0"
Last-Modified: Thu, 09 Jun 2011 10:15:57 GMT
Content-Type: video/x-flv
Content-Length: 7405181
Date: Tue, 13 Sep 2011 11:24:59 GMT
Server: Cisco-CDS
Response: 206
I have seen this resource but it did not help. resource
I have surfed many forums to find an answer but with no success. Any help would be great!
精彩评论