(转载)AS3 HTTPCLIENT读取视频流信息
最近的工作中频繁用到HTTPCLIENT这个类
先放上github的项目地址 https://github.com/gabriel/as3httpclient
原理就是利用socket模拟一个http链接来突破flash player的各种限制
由于是socket连接,所以跨域问题大家还是要好好注意下的
比如原先urlrequest中不能自定义的referer之类的
还有一个妙处就是原先的netstream是不能读取到流的数据,
有时候在流里会藏一些数据,这时候就用到这个类来解决了
这源码是搜索的时候翻到的国外技术,因为原链接已经流失,
俄罗斯战斗民族我又惹不起。。我只好在这里标明我是转载了 =。-
1 private var ns:NetStream; 2 private var video:Video; 3 private var meta:Object; 4 private var client:HttpClient; 5 private var filesize:Number = 0; 6 private var loadedBytes:Number = 0; 7 private var data:ByteArray = new ByteArray(); 8 private var datadelta:Number = 1024*1024; 9 private var file:String = "http://your_video_web_url/example.flv"; 10 11 private function init():void{ 12 var nsClient: Object = {}; 13 nsClient.onMetaData = metaDataHandler; 14 var nc:NetConnection = new NetConnection(); 15 nc.connect(null); 16 ns = new NetStream(nc); 17 ns.client = nsClient; 18 ns.addEventListener(NetStatusEvent.NET_STATUS,netStatusHandler); 19 ns.addEventListener(IOErrorEvent.IO_ERROR,nsIOErrorHandler); 20 video = new Video(); 21 video.attachNetStream(ns); 22 video.smoothing = true; 23 uic.addChild(video); 24 client = new HttpClient(); 25 loadData(); 26 ns.play(null); 27 } 28 private function loadData():void{ 29 var uri:URI = new URI(file); 30 var request:HttpRequest = new Get(); 31 var maxdata:Number = loadedBytes+datadelta; 32 if (maxdata>=filesize and filesize>0){ 33 request.addHeader('Range','bytes='+loadedBytes+'-'); 34 } else { 35 request.addHeader('Range','bytes='+loadedBytes+'-'+maxdata); 36 } 37 38 client.listener.onData = function(e:HttpDataEvent):void 39 { 40 var bytes:ByteArray = new ByteArray(); 41 bytes = e.bytes; 42 bytes.position = 0; 43 data.writeBytes(bytes); 44 }; 45 46 client.listener.onComplete = function(e:HttpResponseEvent ):void{ 47 loadedBytes+=data.length; 48 filesize = Number(e.response.header.getValue('Content-Length'))/1024; 49 ns.appendBytes(data); 50 data.clear(); 51 inLoaded = false; 52 }; 53 54 client.request(uri,request); 55 }