我有一个简单的flv播放器连接到CDN,如果用户想跳过flv而不需要100%加载flv,它可以接受头字节范围请求。
我正在使用第三方http librabry将范围标头添加到请求中。
我的代码播放flv但是当我尝试跳到30秒进入视频时它就冻结了。我相信cdn会返回文件,因为我可以跟踪字节,而标题包含内容范围repsonse(下面)。
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类
...
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);
}
}
....
第一个标题请求和响应
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
寻求标头请求和共鸣
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
我见过这个资源,但没有帮助。 resource
我已经浏览了很多论坛以寻找答案,但没有成功。任何帮助都会很棒!