多次试验发现队列下载效率真的不太好。还是扩展一个Loader 加入了超时的设置,详细代码在跟帖中。 ------------------------------------------------ 前几天应征Flash程序员。被问到为什么Flash有时候Loader下载会无缘无故暂停,既不报超时也不报错。当时没想到为什么?后来被告知正确答案是FlashPlayer的并发下载的Bug。汗 今天自己作了一个队列下载的单例类。目的就是解决并发的问题和使用Loader的易用性。 原理是,生成了一个类的单例,在全局使用。单例中包括一个Timer时间轮训,一个array的下载URL列表,一个加载顺序的策略。很简单 如果感兴趣的朋友可以拿去用,哈哈。欢迎大家和我讨论。 CODE: LoadLine.as
复制内容到剪贴板
代码:
package com.FSC.UI.InterActiveObject.DisplayObjectContainer.LoadLine { import flash.display.MovieClip; import flash.display.Loader; import flash.net.URLRequest; import flash.utils.Timer; import flash.events.*; /**下载队列类 * @ andy pan * @ v1.080506 */ public class LoadLine extends MovieClip { private static var loadLine:LoadLine; private static var key:Boolean=false; private var _loader:Loader= new Loader(); private var _loaderList:Array = new Array(); private var _timer:Timer = new Timer(500); private var _isNowLoading:Boolean=false; //加载失败尝试次数 private var _tryErrorTime:Number=1; //当前加载失败次数 private var _tryErrorNowTime:Number = 0; public function LoadLine() { if( !key ){ throw new Error ("单例,请用 getInstance() 取实例。"); } key=false; _timer.addEventListener(TimerEvent.TIMER,everySed); _timer.start(); _loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onCompleteHandler); _loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onError); _loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onError); _loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressHandler); } public static function getInstance() : LoadLine { if ( loadLine == null ){ key=true; loadLine = new LoadLine(); } return loadLine; } /**静态函数 下载命令 * @param target下载地址 */ public function loadFile(target:URLRequest,emergent:Number=10){ if(emergent==0){ _loaderList.unshift(target); if(_isNowLoading){ //如果有正在加载的项目则 暂停 cancelNowLoad(); } }else{ _loaderList.push(target); } } /**终止当前下载 */ private function cancelNowLoad(){ _loader.close(); } /**每秒监听 队列是否有下载 * @param e 时间参数 */ private function everySed(e:TimerEvent){ //但下载器空闲时 if((!_isNowLoading)&&(_loaderList.length>0)){ var tmp_url:URLRequest = _loaderList[0] as URLRequest; _loader.load(tmp_url); this.addChild(_loader); _isNowLoading = true; } } /**当前下载中的侦听 */ private function progressHandler(e:ProgressEvent){ this.dispatchEvent(new LoadLineProgressEvent(ProgressEvent.PROGRESS,_loaderList[0],false,false,e.bytesLoaded,e.bytesTotal)); } /**当前下载完成 */ private function onCompleteHandler(e:Event){ this.dispatchEvent(new LoadLineEvent(LoadLineEvent.ONLOADLINECOMPLETE,_loaderList[0],_loader.content)); _loaderList.shift();//当完成下载后再从队列中删除 _isNowLoading = false; } /**当加载错误进行尝试处理 */ private function onError(e:IOErrorEvent){ _isNowLoading = false; if(_tryErrorNowTime>_tryErrorTime){ trace("error:下载队列中有文件加载失败,并且超过尝试次数。已从队列中删除") this.dispatchEvent(new LoadLineEvent(LoadLineEvent.ONLOADLINECOMPLETE,_loaderList[0],false)); _tryErrorNowTime=0; //当完成下载后再从队列中删除 _loaderList.shift(); }else{ _tryErrorNowTime++; } } } }
自定义加载完成的事件类 LoadLineEvent.as
复制内容到剪贴板
代码:
package com.FSC.UI.InterActiveObject.DisplayObjectContainer.LoadLine { import flash.events.Event; import flash.net.URLRequest; public class LoadLineEvent extends Event { public static var ONLOADLINECOMPLETE : String = "onLoadLineComplete"; public var currentTragetURL:URLRequest; public var content:*; public var isSuccess:Boolean public function LoadLineEvent(type:String,URL:URLRequest,content,isSuccess:Boolean = true, bubbles:Boolean=true, cancelable:Boolean=false) { super(type, bubbles, cancelable); this.currentTragetURL = URL; this.isSuccess = isSuccess; this.content = content; } } }
自定义加载过程中的事件类 LoadLineProgressEvent.as
复制内容到剪贴板
代码:
package com.FSC.UI.InterActiveObject.DisplayObjectContainer.LoadLine { import flash.events.ProgressEvent; import flash.net.URLRequest; public class LoadLineProgressEvent extends ProgressEvent { public var currentTragetURL:URLRequest; public function LoadLineProgressEvent(type:String,URL:URLRequest, bubbles:Boolean=false, cancelable:Boolean=false, bytesLoaded:uint=0.0, bytesTotal:uint=0.0) { super(type, bubbles, cancelable, bytesLoaded, bytesTotal); this.currentTragetURL = URL; } } }
使用方法如下: import com.FSC.UI.InterActiveObject.DisplayObjectContainer.LoadLine.*; var l:LoadLine = LoadLine.getInstance(); l.addEventListener(LoadLineEvent.ONLOADLINECOMPLETE,onComplete); l.addEventListener(ProgressEvent.PROGRESS,onProgress); //插入可用的url l.loadFile(new URLRequest("http://static16.photo.sina.com.cn/bmiddle/54a5adcd44b992988febf")); //插入错误的url l.loadFile(new URLRequest("http://error.error.cn/error.htm")); //插入可用的url l.loadFile(new URLRequest("http://static3.photo.sina.com.cn/bmiddle/54a5adcd44b9926c6faf2")); //插入可用的url 这里插入紧急级别为0(就是最高级别的)的URL 程序会停止当前下载过程,然后执行该下载 l.loadFile(new URLRequest("https://mail.google.com/mail/help/images/logo.gif"),0);
function onComplete(e:LoadLineEvent) { trace(e.currentTragetURL.url); if (e.isSuccess && e.currentTragetURL.url =="http://static16.photo.sina.com.cn/bmiddle/54a5adcd44b992988febf") { var bbb:* = e.content; bbb.y=0; addChild(bbb); } } function onProgress(event:LoadLineProgressEvent) { trace("progressHandler: bytesLoaded=" + event.bytesLoaded + " bytesTotal=" + event.bytesTotal); }
附件
eg.rar
详细讨论见蓝色理想帖子
http://bbs.blueidea.com/thread-2852727-1-1.html
|