闪存,在实际加载文件访问totalBytes闪存、加载、文件、在实际

2023-09-08 14:54:32 作者:白昼撩妹神.

林寻找一个简单的方法来找到我打算加载的文件的文件大小不加载这些呢。我基本上做一个批处理文件需要被加载,这将是非常有用的,如果我能确定事先有多大的总负荷将是。

Im looking for a simple way to find the file sizes of the files i planning on loading without loading them yet. I'm basically making a batch of files that needs to be loaded and it would be very useful if I could determine on beforehand how big the total load is going to be.

在闪存Loader对象具有totalBytes属性,但只会返回时,你已经加载文件的值。

the Loader object in flash has a totalBytes property, but that will only return a value when you are already loading a file.

推荐答案

通过加载,你的意思是完全加载,或空载通话?如果一个负载呼叫是可以接受的,以下可能是有用的。进度事件给出的第一把火输入数据的总大小。我们只是让一个给定的进度事件火灾,当前加载项的总价值,并添加到自己的_total变量;我们将继续,直到我们的数据提供器(阵列,XML等)是空的。年底你有你的所有资产的总和。

By loaded, do you mean entirely loaded, or with no load call? If a load call is acceptable, the following could be useful. The progress event gives the incoming data's Total size on the first fire. We merely let a given progress event fire, take the total value of the current loading item and add that to our own _total variable; We continue this until our dataprovider (array, xml, etc) is empty. by the end you have the sum of all your assets totals.

package
{
    import flash.display.Sprite;
    import flash.display.Loader;
    import flash.events.Event;
    import flash.events.ProgressEvent;
    import flash.URLLoader;
    import flash.URLRequest;

    public class ExampleDocumentClass extends Sprite
    {
        private var _urlLoader:URLLoader;
        private var _loader:Loader;
        private var _request:URLRequest;
        private var _total:Number;
        private var _array:Array;

        public function ExampleDocumentClass():void
        {
            if(stage) _init;
            else addEventListener(Event.ADDED_TO_STAGE, _init);
        }
        private function _init(e:Event = null):void
        {
            _array = [{type:"nonDisplay", path:"path/to/file"},{type:"display", path:"path/to/file"}];
            _urlRequest = new URLRequest();

            //Non display asset
             _urlLoader = new URLLoader();
            _urlLoader.addEventListener(ProgressEvent.PROGRESS, _onProgress);

            //Display asset
            _loader = new Loader();

            loadAsset();
        }
        private function loadAsset():void
        {
            if(_array.length > 0)
            {
                var pObj:Object = _array[0];
                if(pObj.type == "nonDisplay")
                {
                    loadNonDisplayAsset(pObj.path);
                }
                else
                {
                    loadDisplayAsset(pObj.path);
                }
                //Remove this item from the array;
                _array.shift();
            }
        }
        private function loadNonDisplayAsset(pAsset:String):void
        {
            _urlRequest.url = pAsset;
            _urlLoader.load(_urlRequest);
        }
        private function loadDisplayAsset(pAsset:String):void
        {
            _urlRequest.url = pAsset;
            _loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, _onProgress);
            _loader.load(_urlRequest);
        }
        private function _onProgress(e:ProgressEvent):void
        {
            _total += e.bytesTotal;
            _loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, _onProgress);
            _urlLoader.removeEventListener(ProgressEvent.PROGRESS, _onProgress);
        }
    }
}

因此​​,我们加载资源足够长的时间来追加其规模总量,我们自己_total变量,然后我们调用数组中的下一个项目,并追加其总直至数据提供程序是空的。显然,这可以使用XML完成,或者无论你想,一个数组是我用正是行使。

So we load an asset just long enough to append its Size total, to our own _total variable and then we call the next item in the array and append its total until the data provider is empty. Obviously this could be done with XML, or however you wish, an array is just what i used for the exercise.

布赖恩·霍奇 hodgedev.com blog.hodgedev.com

Brian Hodge hodgedev.com blog.hodgedev.com