Flash,在实际加载文件之前访问totalBytes

发布于 2024-07-13 23:20:45 字数 154 浏览 3 评论 0原文

我正在寻找一种简单的方法来查找我计划加载的文件的文件大小,而无需加载它们。 我基本上正在制作一批需要加载的文件,如果我可以事先确定总负载有多大,那将非常有用。

flash 中的 Loader 对象有一个totalBytes 属性,但只有当您已经加载文件时才会返回一个值。

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

不乱于心 2024-07-20 23:20:45

所谓加载,你的意思是完全加载,还是没有加载调用? 如果加载调用可以接受,则以下内容可能有用。 进度事件在第一次触发时给出传入数据的总大小。 我们只需触发给定的进度事件,获取当前加载项的总值并将其添加到我们自己的 _total 变量中; 我们继续这个操作,直到我们的数据提供者(数组、xml 等)为空。 最后你就得到了所有资产的总和。

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 来完成,或者无论您希望什么,数组正是我在练习中使用的。

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);
        }
    }
}

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.

残疾 2024-07-20 23:20:45

以前无法从 Actionscript 3 获取文件的大小。 一个简单的解决方案是向服务器添加一个小脚本,该脚本扫描所有文件并将其名称和大小返回到 swf(作为 JSONXML 或其他形式) 。

There is no way to getting the size of a file before from Actionscript 3. A easy solution would be to add a small script to the server which scans for all files and returns their names and their size to the swf (as JSON or XML or whatever).

水中月 2024-07-20 23:20:45

在 ActionScript 中没有办法做到这一点。 您需要一些服务器端代码来执行此操作。 您可以创建一个接受文件名并返回总字节数的方法。

There is no way to do this in ActionScript. You'll need some server side code to do this. You could create a method that takes a filename and returns the total bytes.

献世佛 2024-07-20 23:20:45

我不知道你会如何使用actionscript(我不是专家)来做到这一点,但我的第一个猜测是使用PHP脚本(或你选择的任何其他脚本语言)来获取文件大小并返回值。

I don't know how you would do this with actionscript (I'm no Guru) but my first guess would be to use a PHP script (or any other scripting language of your choice) to get the file sizes and return the values.

黯然#的苍凉 2024-07-20 23:20:45

另一种(可能是过度的)方法是创建一些简单地向服务器询问文件头(包括文件大小)的东西。
可以通过简单地开始下载文件并在获得文件大小(应该很快)时关闭流或使用(如之前建议的)其他方式向服务器询问文件的大小(如果您正在加载多个文件,并且需要在加载之前知道文件大小)

Another (probably overkill) way would be to create something that simply asks the server for the headers of the file (including the filesize).
The same could be achieved by simply starting to download the file and closing the stream when you have the filesize (should be pretty fast) or having (as suggested before) some other way of asking the server for the size for the file (useful if you're loading multiple files and need to know the filesize before loading)

滿滿的愛 2024-07-20 23:20:45

我已经尝试过 Brian Hodge 提供的方法,以及另一种方法,其中我将变量(XML 中的字符串形式的文件夹位置)发送到 PHP,以便获取特定文件夹内所有文件的组合大小并将数据返回到闪光。

我在预加载天堂之旅中遇到的一个主要问题是 Flash Player 似乎在文件大小方面产生不一致的结果! 即使我加载完全相同的文件两次,Flash 也会告诉我它们的大小不同......有时。

这也导致了替代 (PHP) 方法的问题,因为 PHP 脚本会生成(看似)准确的文件大小结果,但与 Flash 生成的结果不一致,导致我的加载程序达到 85% 或 128%,具体取决于缓存和Flash 的心情:/

我放弃了这两种方法,转而使用一个名为 bulk-loader,它允许排队加载和跟踪多个项目的进度,等等 :D

请注意,它会显着增加您的 swf 文件大小...多么讽刺

I have experimented with the method provided by Brian Hodge, as well as another method in which I sent variables (folder locations as strings from XML) to PHP in order to get the combined size of all files within a particular folder and return the data to Flash.

One major problem I encountered on my journey to preloading paradise was that Flash Player seems to produce inconsistent results with regard to file size! Even if I loaded exactly the same files twice Flash would tell me they were different sizes... sometimes.

This also caused problems with the alternate (PHP) method, since the PHP script would produce (seemingly) accurate file size results that were inconsistent with those produced by Flash, causing my loader to go to 85% or 128%, dependant on caching and Flash's mood :/

I scrapped both methods in favour of a nice class called bulk-loader, which allows for queued loading and progress tracking of multiple items, and much more :D

Just be aware that it will increase your swf file size significantly... how ironic

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文