·您当前的位置:首页 > 技术教程 > AS2与AS3技术 >

[AS3]as3简单渐进式加载图片类ByteLoader

时间:2014-11-18 10:56酷播
你可能会说“我的目的很简单,我只是要加载一张图片,用渐进的方式边加载边显示,我可不知道哪里有一串字符串让我去加载。嗯,来看看这个类吧,ByteLoader继承Loader类,使用方法跟Loader类一样简单

你可能会说“我的目的很简单,我只是要加载一张图片,用渐进的方式边加载边显示,我可不知道哪里有一串字符串让我去加载。嗯,来看看这个类吧,ByteLoader继承Loader类,使用方法跟Loader类一样简单。

原理是:

用URLStream类去加载图片数据流,
然后用帧循环stream.readBytes(data, data.length)将数据读入ByteArray中;
同时在帧循环中卸载Loader中旧的数据,加载新的数据super.unload();super.loadBytes(data);
代码如下:

  1. package { 
  2.         import flash.events.EventDispatcher; 
  3.         import flash.events.ProgressEvent; 
  4.         import flash.events.Event; 
  5.         import flash.utils.ByteArray; 
  6.         import flash.net.URLStream; 
  7.         import flash.net.URLRequest; 
  8.         import flash.display.Loader; 
  9.         public class ByteLoader extends Loader{ 
  10.                 public var url:String; 
  11.                 public var data:ByteArray; 
  12.                 private var stream:URLStream; 
  13.                 public function ByteLoader(url:String = ""){ 
  14.                         if(url != ""){ 
  15.                                 loadUrl(url); 
  16.                         } 
  17.                 } 
  18.                 //CuPlayer.com 加载 
  19.                 public function loadUrl(_url:String):void{ 
  20.                         url = _url
  21.                         data = new ByteArray; 
  22.                         stream = new URLStream; 
  23.                         stream.load(new URLRequest(url)); 
  24.                         stream.addEventListener(Event.COMPLETE,completeFun); 
  25.                         stream.addEventListener(ProgressEvent.PROGRESS,progressFun); 
  26.                         addEventListener(Event.ENTER_FRAME,loaddata); 
  27.                 } 
  28.                 private function loaddata(e:Event):void{ 
  29.                         if (stream.bytesAvailable > 0){ 
  30.                                 stream.readBytes(data, data.length); 
  31.                         } 
  32.                         if (data.length > 0){ 
  33.                                 super.unload(); 
  34.                                 super.loadBytes(data); 
  35.                         } 
  36.                 } 
  37.                 // CuPlayer.com 加载中 
  38.                 private function progressFun(erogressEvent):void{ 
  39.                         if(stream.bytesAvailable == 0) return; 
  40.                         dispatchEvent(e); 
  41.                         trace(e.bytesLoaded); 
  42.                 } 
  43.                 //CuPlayer.com 加载完成 
  44.                 private function completeFun(e:Event):void{ 
  45.                         stream.removeEventListener(Event.COMPLETE,completeFun); 
  46.                         stream.removeEventListener(ProgressEvent.PROGRESS,progressFun); 
  47.                         dispatchEvent(e); 
  48.                         this.closeStream(); 
  49.                 } 
  50.                 //CuPlayer.com 清除数据 
  51.                 public function closeStream():void{ 
  52.                         if (stream){ 
  53.                                 if (stream.connected) stream.close(); 
  54.                         } 
  55.                         if (hasEventListener(Event.ENTER_FRAME)){ 
  56.                                 removeEventListener(Event.ENTER_FRAME, loaddata); 
  57.                         } 
  58.                 } 
  59.         } 

 

热门文章推荐

请稍候...

保利威视云平台-轻松实现点播直播视频应用

酷播云数据统计分析跨平台播放器