To download the code, you must agree to the following license:

Copyright 2010, GreenSock, Inc.

"NO CHARGE" NON-EXCLUSIVE SOFTWARE LICENSE AGREEMENT
-----------------------------------------------------------------------------
PLAIN ENGLISH SUMMARY:

  1. You may use the code at no charge in commercial or non-commercial web sites, games, components, applications, and other software as long as end users are not charged a fee of any kind to use your product or gain access to any part of it. If your client pays you a one-time fee to create the site/product, that's perfectly fine and qualifies under the "no charge" license. If end users are charged a usage/access/license fee, please sign up for a corporate Club GreenSock membership which comes with a special commercial license granting you permission to do so. See http://www.greensock.com/club/ for details.
  2. Use at your own risk. No warranties are offered.
  3. Please respect the copyright.

-----------------------------------------------------------------------------

LEGALESE:

This is a legal agreement between you (either an individual or a single entity) and GreenSock, Inc. ("GREENSOCK") for the proprietary GreenSock ActionScript code known as TweenLite, TweenMax, TweenNano, TimelineLite, TimelineMax, and other code that is available for download at http://www.greensock.com (this code and documentation, as well as any updates which may at GREENSOCK's sole discretion be provided to you from time to time, are referred to in this Agreement as "PROGRAM") By downloading, copying, or otherwise using the PROGRAM, you agree to the terms and conditions of this Agreement. If you do not agree to the terms and conditions of this Agreement, please do not download or use the PROGRAM.

I. LICENSE
A. Subject to the terms and conditions of this Agreement, GREENSOCK hereby grants you a non-exclusive, worldwide, non-transferable right to use the PROGRAM in web sites, games, components and other software applications for which the end user is NOT charged any fees. If you would like to use the code in a commercially licensed software product for which end users are charged a fee (either for usage or access), simply sign up for a corporate Club GreenSock membership at http://www.greensock.com/club/.

II. LIMITATION OF LICENSE AND RESTRICTIONS
A. You agree that you will not disclose, sell, rent, or license the PROGRAM's source code or any derivative works thereof to any third party without the prior written consent of GREENSOCK. Derivative works are defined as modifications that add substantive functionality to the PROGRAM and do not include bug fixes or other minor modifications required to operate the PROGRAM as originally intended. Distribution of the source code as part of your Work Product is acceptable so long as the recipients agree to the terms of this Agreement. You agree not to modify or delete GREENSOCK'S existing copyright notice located in the source code.

B. You may use, duplicate, and distribute the compiled object code as embedded in a Work Product created by you, either for your own use or for distribution to a third party so long as end users of the Work Product are not charged a fee for usage of or access to any portion of the Work Product. Please see http://www.greensock.com/licensing/ for descriptions of Work Products that qualify for the "No Charge" license.

III. CONSIDERATION
A. The license rights granted to you under this Agreement are at no charge, but only in the following circumstances: If on your own behalf or on behalf of a third party you incorporate the PROGRAM into a web site, game, software application, program or any component thereof (collectively, "Work Product"), which in the case of a web site, must be accessible to internet users without payment of a fee of any kind, and in the case of a software application, game, program or component, neither you nor anyone to whom you distribute the Work Product charges a user a fee of any kind to use such Work Product or application, game, program or component into which such Work Product is embedded. The foregoing shall apply regardless of whether you are paid to create such Work Product.

B. In the event your intended use of the PROGRAM does not meet the criteria for the "no charge" license rights set forth in the immediately preceding paragraph, then you are not licensed to use the PROGRAM under this Agreement and must license the PROGRAM under GREENSOCK'S separate fee-based Software License Agreement which is granted to corporate Club GreenSock members (see http://www.greensock.com/club/ for details).

IV. TITLE AND OWNERSHIP
A. The PROGRAM is licensed, not sold, and is protected by copyright laws and international treaty provisions. You acknowledge that no title to the intellectual property in the PROGRAM is transferred to you. You further acknowledge that title and full ownership rights to the PROGRAM, including all intellectual property rights therein, will remain the exclusive property of GREENSOCK and you will not acquire any rights to the PROGRAM except as expressly set forth in this Agreement. You agree that any copies of the PROGRAM you make will contain the same proprietary notices which appear on and in the PROGRAM. You agree that GREENSOCK may identify you as a licensee unless you make a written request otherwise. GREENSOCK hereby grants to you the right to disclose that your product, game, software application, component, or other Work Product makes use of GREENSOCK code (for example, "Powered by TweenLite").

V. DISCLAIMER OF WARRANTY AND LIMITATION OF LIABILITY
A. THE PROGRAM IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. GREENSOCK DOES NOT WARRANT THAT THE FUNCTIONS CONTAINED IN THE PROGRAM WILL MEET YOUR REQUIREMENTS OR THAT OPERATION WILL BE UNINTERRUPTED OR ERROR FREE. GREENSOCK shall not be liable for special, indirect, incidental, or consequential damages with respect to any claim on account of or arising from this Agreement or use of the PROGRAM, even if GREENSOCK has been or is hereafter advised of the possibility of such damages. Because some states do not allow certain exclusions or limitations on implied warranties or of liability for consequential or incidental damages, the above exclusions may not apply to you. In no event, however, will GREENSOCK be liable to you, under any theory of recovery, in an amount in excess of $250. Notwithstanding anything else in this agreement, you agree to indemnify GREENSOCK, its assignees, and licensees, and hold each of them harmless from and against any and all claims, demands, losses, damages, liabilities, costs, and expenses, including legal fees resulting from your use of the PROGRAM.

B. GREENSOCK may, at its sole discretion, provide support services related to the PROGRAM, but has no obligation to do so.

VI. TERMINATION
If you at any time fail to abide by the terms of this Agreement, GREENSOCK shall have the right to immediately terminate the license granted herein, require the return or destruction of all copies of the PROGRAM from you and certification in writing as to such return or destruction, and pursue any other legal or equitable remedies available.

VII. MISCELLANEOUS
A. This Agreement shall be construed in accordance with the laws of the State of Illinois. In the event of any dispute between you and GREENSOCK with respect to this Agreement, we both agree that if we cannot resolve the dispute in good faith discussion, either of us may submit the dispute for resolution to arbitration with the American Arbitration Association before a single arbitrator using the AAA Rules for Commercial Arbitration. The arbitrator's decision is final and can be enforced in any court with jurisdiction over such matters.

B. This agreement represents the complete and exclusive statement of the agreement between GREENSOCK and you and supersedes all prior agreements, proposals, representations and other communications, verbal or written, between them with respect to use of the program. This agreement may be modified only with the mutual written approval of authorized representatives of the parties.

C. The terms and conditions of this Agreement shall prevail notwithstanding any different, conflicting, or additional terms or conditions which may appear in any purchase order or other document submitted by you. You agree that such additional or inconsistent terms are deemed rejected by GREENSOCK.

D. GREENSOCK and you agree that any xerographically or electronically reproduced copy of this Agreement shall have the same legal force and effect as any copy bearing original signatures of the parties.

I'd like to learn how to get bonus plugins, update notifications, SVN access, and more.
To join Club GreenSock, you must agree to the following license:

PreloadAssetManager (AS2) – An Intelligent Preloading Queue

  • Version: 2.93, Updated 3/31/2008
  • Compatibility: AS2, Flash Player 6 and later (version 7 for FLV preloading)
  • File size added to compressed SWF: about 3kb

Download Now
 
Donate

NOTE: PreloadAssetManager has been sunset and is no longer actively enhanced or supported. Feel free to use it if you want, though.

Description

Provides an easy way to invisibly preload SWFs, FLVs, or images and optionally trigger a callback function when preloading has finished. It also provides _width and _height information for all successfully preloaded SWFs or images, and duration information for FLVs (assuming they were encoded properly and have MetaData). By default, it will initially only load enough of each asset to determine the size (bytes) of each asset so that it can accurately report the percentLoaded_num, getBytesLoaded() and getBytesTotal(), then it loops back through from the beginning and finishes all the preloading. If you're not going to use a preloader status bar that polls these methods/properties, you can just set the trackProgress_boolean property to false to skip that initial delay.

Forums

Objectives

  • Provide an easy way to sequentially preload assets.
  • Determine the _width and _height of any preloaded SWFs or image assets as well as the duration of any FLV assets.
  • For easy status reporting, provide percentLoaded_num, getBytesLoaded() and getBytesTotal() information for the entire group of assets in any PreloadAssetManager instance.
  • Allow for any asset to be prioritized in the loading queue at any time (for example, if the user clicks on something that requires an asset that hasn't loaded yet).
  • Allow the entire queue to be paused and resumed at any time (if, for example, you need to perform some other bandwidth-intensive action)
  • Build in the ability to call any function when either a particular asset has finished preloading or when a group of assets have finished preloading and allow the developer to pass any number of arguments/parameters to that function.
  • If an asset cannot be loaded, implement a timeout procedure so things don't get hung up indefinitely.

Usage

Constructor: new PreloadAssetManager(assetUrls_array:Array, onComplete_func:Function, onCompleteArguments_array:Array, trackProgress_boolean:Boolean);
  • Description: Constructor. If you pass in an assetUrls_array, it will automatically call the start() function and begin preloading. You can pause() if you want.
  • Arguments:
    1. assetUrls_array: [optional] An array of urls that should be preloaded
    2. onComplete_func: [optional] A reference to a function that you'd like to call as soon as all of the assets in this PreloadAssetManager instance have been preloaded.
    3. onCompleteArguments_array: [optional] An array of arguments to pass the onComplete_func function.
    4. trackProgress_boolean: [optional] true by default. If you're NOT going to use a preloader status bar that polls the precentLoaded_num, getBytesLoaded() or getBytesTotal(), set this value to false to skip that initial delay and speed things up.
addAsset(url_str:String, onComplete_func:Function, onCompleteArguments_array:Array):PreloadAsset
  • Description: Adds an asset to the PreloadAssetManager's queue.
  • Arguments:
    1. url_str: The url of the asset that needs to be preloaded
    2. onComplete_func: [optional] A reference to a function that you'd like to call as soon as this asset has been preloaded.
    3. onCompleteArguments_array: [optional] An array of argument values to pass the onComplete_func function.
addAssets(assetUrls_array:Array, onComplete_func:Function, onCompleteArguments_array:Array, start_boolean:Boolean):Array
  • Description: Adds an asset to the PreloadAssetManager's queue.
  • Arguments:
    1. assetUrls_array: An array of URLs that need to be preloaded
    2. onComplete_func: [optional] A reference to a function that you'd like to call as soon as all of the assets in this PreloadAssetManager have been preloaded.
    3. onCompleteArguments_array: [optional] An array of argument values to pass the onComplete_func function.
    4. start_boolean: [optional] false by default. If true, the PreloadAssetManager will be forced to start preloading as soon as these assets have been added.
start()
  • Description: Starts preloading the assets (same as resume() if things are paused).
resume()
  • Description: Resumes all preloading.
pause()
  • Description: Pauses preloading in ALL PreloadAssetManagers.
PreloadAssetManager.prioritize(url_str:String):PreloadAsset
  • Description: A STATIC method that allows you to prioritize a particular asset (bump it up to the top of the queue). You can also use the non-static prioritizeAsset() method or the PreloadAsset.prioritize() method to perform a similar action.
  • Arguments:
    1. url_str: The URL of the asset that should be prioritized.
PreloadAssetManager.getAsset(url_str:String):PreloadAsset
  • Description: A STATIC method that allows you to find the PreloadAsset instance with a particular URL. This is useful if you want to find the _width or _height or duration of an asset but only know the URL.
  • Arguments:
    1. url_str: The URL of the asset you'd like to find.

Examples

To preload 2 SWFs ("myFile1.swf" and "myFile2.swf") and then call a function named onFinish(), do:

Actionscript:
  1. import gs.dataTransfer.PreloadAssetManager;
  2. var preloader_obj = new PreloadAssetManager(["myFile1.swf","myFile2.swf"], onFinish);
  3. function onFinish(pl_obj:PreloadAssetManager):Void {
  4.     trace("Finished preloading all " + pl_obj.assets_array.length + " assets!");
  5.     var a = pl_obj.assets_array;
  6.     for (var i = 0; i <a.length; i++) {
  7.         if (a[i].fileType_str == "flv") {
  8.             trace("--Asset: "+a[i].url_str+" had a duration of: "+a[i].duration);
  9.         } else {
  10.             trace("--Asset: "+a[i].url_str+" had a width of: "+a[i]._width+", and a height of "+a[i]._height);
  11.         }
  12.     }
  13. }

Or if you want to have more granular control, you can create add each one to the PreloadAssetManager and then query properties like preloaded_boolean, _width, or _height individually like:

Actionscript:
  1. import gs.dataTransfer.PreloadAssetManager;
  2. import gs.dataTransfer.PreloadAsset;
  3. var preloader_obj = new PreloadAssetManager();
  4. var pl1_obj = preloader_obj.addAsset("myFile1.swf", onPreload);
  5. var pl2_obj = preloader_obj.addAsset("myFile2.swf", onPreload);
  6. preloader_obj.start();
  7. function onPreload(pl_obj:PreloadAsset):Void {
  8.     trace("finished preloading: "+pl_obj.url_str+", _width: "+pl_obj._width+", _height: "+pl_obj._height);
  9. }

You can query the percentLoaded_num property to find out the status at any time (very useful for building
preloader progress bars) like so:

Actionscript:
  1. import gs.dataTransfer.PreloadAssetManager;
  2. var preloader_obj = new PreloadAssetManager(["myFile1.swf","myFile2.swf"]);
  3. this.onEnterFrame = function() {
  4.     myPreloader_mc.bar_mc._xscale = preloader_obj.percentLoaded_num;
  5.     if (preloader_obj.percentLoaded_num == 100) {
  6.         gotoAndPlay("start");
  7.     }
  8. }

Need help?

Feel free to post your question on the forums. You'll increase your chances of getting a prompt answer if you provide a brief explanation and include a simplified FLA file (and any class files) that clearly demonstrates the problem.

Donate
Author: Jack Doyle
Copyright 2010, GreenSock (This work is subject to the terms here.)

Comments (25) RSS

Posted by laurent on February 7, 2007

Great Class!

How do I resolve the scope in an onFinish function?

coverMC = target.createEmptyMovieClip(“cover”,target.getNextHighestDepth());
var preload_obj = new PreloadAsset(coverImage, onFinish);
function onFinish(asset_obj:PreloadAsset):Void {
trace(“Finished preloading: “+ asset_obj.url_str+” and its width is: “+asset_obj._width+” and its _height is: “+asset_obj._height);
trace(this);
// here I want to load in coverMC
}

how do I load the preloadAsset into coverMC when onFinish is called?

Posted by jack on February 7, 2007

If you want to control the scope of the onFinish call, I’d recommend using the mx.utils.Delegate class that comes with Flash. Then to load your asset into coverMC, you could use something like loadMovie() So your edited code would be:

import mx.utils.Delegate;
coverMC = target.createEmptyMovieClip(”cover”,target.getNextHighestDepth());
var preload_obj = new PreloadAsset(coverImage, Delegate.create(this, onFinish));
function onFinish(asset_obj:PreloadAsset):Void {
trace(”Finished preloading: “+ asset_obj.url_str+” and its width is: “+asset_obj._width+” and its _height is: “+asset_obj._height);
trace(this);
coverMC.loadMovie(asset_obj.url_str);
}

Posted by leolea on April 4, 2007

Preloading works super fine! Thank you very much for this class.

I am preloading a bunch of FLVs. Once they get at 100%, how do I access them using a NetStream object (or other) ?

(I need to implement something that initates the playback of the first one, when it reaches the end I must start the second one, so on…)

Posted by jack on April 4, 2007

Leolea, this class simply preloads your assets into your browser’s cache; it is not meant to be used to play back and manage your FLVs once they’re preloaded. There’s nothing special that you need to do in order to access the preloaded FLVs – just call them as you normally would either using a NetStream object of your own or an FLVPlayback component or whatever. The user’s browser will be smart enough to used the cached versions instead of going out to the web and downloading them again.

Posted by Gabriel on April 25, 2007

Hmmm. A little customization might be necessary for streaming FLV handling. An ideal FLV preloader might wait until the currently-streaming FLV is fully cached before loading an FLV in the background.

If the user requests a new FLV, it would ideally halt the bg-downloading stream and resume it after the requested FLV was fully cached. That assumes it’s possible to halt (and resume) a stream?

If the user requests the background-loading FLV, it’d need to be swapped into the playback area without re-starting that netstream from the beginning.

Has anyone out there tried something like this?

Posted by jack on April 25, 2007

Gabriel, the issues you bring up are exactly why I built this class. It’s very easy to pause preloading and resume it anytime you want (see the pause() and resume() functions) so that you can manage the user’s bandwidth efficiently. You can also prioritize any asset anytime based on what the user clicks on. So if asset #2 is currently preloading, but the user clicks on something that requires asset #6 (which hasn’t been preloaded yet), you can prioritize it immediately (see the prioritize() function).

Posted by Gabriel on April 25, 2007

I definately like thought of being able to prioritize, pause and resume the loading of assets.

After a little investigating, it seems like Flash can’t resume downloading a half-cached FLV? It just starts the stream over from scratch. This can make pausing and resuming the queue a frustrating experience–if the currently-playing clip ends before the preloading clip is finished, the preloading clip is flushed and then starts loading again from the beginning.

It’s not a flaw in your preloader, so much as it is a difficulty with FLV caching. Ideally, we should be able to pause the video stream itself (pause buffering), then resume it later (resume buffering). If we could drop the currently-preloading clip into the video object without re-starting the stream (allowing it continue buffering from where it is), but still start playing from the beginning, we’d be set.

Posted by Nicolas Elizaga on August 10, 2007

Hi! This class has made my life much easier… I have a couple of questions, though:

1. Is there a way to get the width/height of the FLV, alongside the duration?
2. Do you anticipate adding an XML option to this class? For example, the ability to load ANY kind of file… images, xml, swfs, flvs, etc.

Thanks, great job!

-nick

Posted by jack on August 14, 2007

Nicolas, I just added the ability for the width, height, videodatarate, framerate, audiodatarate, and basically all of the available meta data in an FLV to be read in when it preloads. The sample is updated too. Keep in mind that not all FLVs have this meta data available – it depends on the software you used to encode the FLV. Sorenson Squeeze seems to do a good job, but several other software packages omit most of the meta data in which case this class obviously won’t be able to grab it.

Enjoy!

Posted by Graydon on September 25, 2007

Great class.

Implemented it in a project we just did requiring preloading a number of FLVs (user has to disconnect their internet connection in the middle of a set of videos). Worked like a charm.

Thanks.

Posted by Krom on October 5, 2007

Is there a way to destroy the PreloadAssetManager object or will simply deleting the var kill any preloads that may currently exist?

Thank you!

Posted by jack on October 5, 2007

Krom, you can call the destroy() method on your PreloadAssetManager instance. Or if you want to pause ALL preloading that’s being handled by any/all PreloadAssetManagers, just call the static PreloadAssetManager.pauseAll() method.

Posted by jack on October 29, 2007

Please keep in mind that PreloadAssetManager is meant to preload assets, not buffer video. So if you have a 10 minute FLV and want to buffer 2 minutes of it before playing it, use a NetStream and its bufferTime property instead of PreloadAssetManager. My class simply leverages the browser’s cache to do its magic, but in order to work properly, it needs to fully download an asset. If you download 20% of an FLV using PreloadAssetManager and then try to play that on the screen, Flash will request the file from the browser which will in turn check its cache and since it’s not fully there, it’ll make the request from the server and start the load over again. Not ideal by any means.

Posted by mark on November 7, 2007

Very useful.
Is there any way to get the percentLoaded_num of an individual asset?
I want to build separate progress bars for each asset.

Posted by jack on November 7, 2007

Mark,

You can check the progress of any asset after you’ve added it to the PreloadAssetManager. For example, to check the status of a file named “myURL.swf”, do something like:

var asset_obj = PreloadAssetManager.getAsset(“myURL.swf”);
var percentLoaded_num = (asset_obj.getBytesLoaded() / asset_obj.getBytesTotal()) * 100;

Posted by Stephen on December 4, 2007

I’m just now starting to learn some of the more advanced functions of actionscript and I’m pretty sure that I’ve got the hang of this class but I have a question that will hopefully clear some things up for me.

Say I’ve got a movieclip that loads a random jpg from a list as a “background” so, in order to make sure that the background comes up without a hitch, I have to have all of the backgrounds loaded. My question is whether or not I should use loadMovie to get the jpg into the movieclip or some other function now that the jpg has already been preloaded. Also, should I be referring to the PreloadAssetManager’s array_asset for the src of the image now or just stick with the original url?

I may just be missing something but, I’ll preload the backgrounds and when I go to load the image, I notice in the bandwidth profiler that the swf has requested the file again even though the file should be cached.

Whatever help you can provide would be greatly appreciated.

Posted by jack on December 4, 2007

Yes, you’ve got it right, Stephen – you need to use loadMovie() (or its equivalent) to load your background images into place. PreloadAssetManager is ONLY meant to get the assets into your browser’s cache, nothing more. And that also explains why Flash’s bandwidth profiler is requesting the file again. That’s to be expected, and that’s exactly what the browser does too, but since it checks its cache FIRST before hitting the server again, it finds the asset(s) and loads them from there for much snappier performance. Again, PreloadAssetManager leverages the BROWSER’S cache but Flash’s bandwidth profiler doesn’t.

Posted by Banta67 on January 3, 2008

Thank Goodness.

Spent the whole day looking for a sollution. This one works.

YOU ROCK!

Posted by tom d on January 26, 2008

awesome, i was starting to implement a simple version of this and happened across yours, does everything i want and does it very well :) will be sure to let you know of the project url when it’s done! thank you for saving my time :)

Posted by ekion on January 28, 2008

Hi,
I would like to know if this preloading asset exist for AS3 ?
if it doesn’t, will u think about converting this asset for AS3 ?

Thanks for ur time if you could answer on my email. it would be great.

Posted by jack on January 30, 2008

I’ll probably port this to AS3 when I get a project that requires it. I’m not entirely sure when that’ll be though.

Posted by Danny on March 11, 2008

Jack, thanks for the code. I got it to work (took a while because I’m novice and never worked with custom classes before). I’ll be spreading the good word about your blog and generous sharing. Thanks again.

Posted by Juan on April 7, 2008

Buddy you are a genius. This is by far the best preloader I’ve come across.

Posted by oyster on May 9, 2008

Hi,
RE AS3 integration.

I guess we can use this on a preload page to cache as3 assets.

ie
preload.html
_as2 preload assets (AS3.swfs)
_onComplete ( getURL( “next.html” )
next.html
_AS3 assets

yes?
Thanks i love this class. I train my students to use it in managing their flash frameworks.
Oyster

Posted by Mark Gason on January 25, 2009

for some who were looking for a AS3 solution, I just found this
http://code.google.com/p/bulk-loader/

it looks pretty good, I have not dug into it seriously yet.

Hope you do not mind me posting an alternative Jack, but it seemed from your comments that you were not likely to be doing an AS3 solution anytime soon.