Preload assets with JavaScript, load-complete callback for single assets include SWF/Flash

Posted on | Mai 15, 2010 | 4 Comments

Currently i need to know within JavaScript when a SWF-file was loaded completly. You could easily handle this with a single ExternalInterface call from within the Flash-application. This is not a very nice solution if this has to be done for each of you flash-applications. This is the neccesary way if you also have to access the flash-application from outside. But in my case i just need to know when the flash is loaded and available in the browser cache.

For this i wrote the swfJSPreLoader. This preloader accepts an array of assets you would like to preload. Internally it will create an flash-application which does the preloading. You could define several callback handlers. So JavaScript gets to know when all assets are loaded or each single asset is loaded including SWF-files.

<script type="text/javascript">
	var swfJSPreLoaderConfig = {
		'_swfPath':'./', // configure path to preloader SWF file
 
		'assets':[	'assets/teaser.swf',
				 	'assets/img2.jpg',
				 	'--error--'],
 
		'assetLoaded': function( asset, bytes, status ){
			alert( asset + ' loaded (' + bytes + ')' );
		},
 
		'loadComplete': function(){ 
			alert('all assets loaded. totally loaded bytes: ' + swfJSPreLoader.loadedBytes() ); 
		},
 
		'loadError': function(){ 
			alert('load error'); 
		},
 
		'callback': function(){
			swfJSPreLoader.addAsset('assets/teaser2.swf',function( status ){ 
															alert('teaser2 loaded status:' + status ); 
														});
		}
	}
</script>
 
<script type="text/javascript" src="swfobject.js"></script>
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="swfJSPreLoader.js"></script>

To use the swfJSPreLoader you have to include three js files for swfObjects, JQuery and the swfJSPreLoader.js. You also have to define an JS variable swfJSPreLoaderConfig to configure the preloader. The config-variable could hold the following items:

  • assets: assets is an array of the assets which should be loaded. The asset-paths must be relative the html page. If you insert wrong asset pathes you could define an error handler. The assets array is optional. You could also add assets via method call.
  • assetLoaded: assetLoaded defines the callback method for each loaded asset. So this method will be called each time an asset is loaded completly. There are three parameters. asset – gives the asset-path; bytes – gives the number of loaded bytes of the current asset; status – gives the loading status ( 200: Ok, 404: Loading error )
  • loadComplete:The loadComplete callback method will be called after the queue of assets to load is empty. Not all assets need be loaded successfully. If not the error-callback will be fired.
  • loadError:The loadError callback will be triggered if the current asset could not be loaded correctly.
  • callback:With callback you could register a method which is called after the initialization of the swfJSPreLoader. After this method is called you could use the public methods of the pre loader.

There are two public methods of the swfJSPreLoader. Both methods are ready after the callback method is called and the initialisation is ready ( the loader flash is instanziated ).

swfJSPreLoader.addAsset: The addAsset method allows you to add a single asset to add to the loading queue. It takes to parameters. The first is the path to the asset, the second is the callback handler, which will be fired after this specific asset was loaded. The callback will also be called if the loading fails. The callback gets one argument status with the loading status ( 200: Ok, 404: Loading error ).

/**
 * @public 
 * 
 * adds asset to loading queue
 * @param {String} assetPath - path to assets to load
 * @param {Function} callback - optional callback handler when loading is complete
 */
addAsset:function( assetPath, callback )
swfJSPreLoader.addAsset('assets/teaser2.swf',
						function( status ){ 
							alert('teaser2 loaded status:' + status ); 
						});

swfJSPreLoader.loadedBytes: The loadedBytes method returns the number of total loaded bytes.

Update 2009-05-17:swfJSPreLoader Flash Builder Project Sourcefiles (ZIP, 553kb)

Update 2009-05-17:swfJSPreLoader-Test (ZIP, 532kb)

I hope this is helpfull for anybody.

cheers,
Sebastian

Comments

4 Responses to “Preload assets with JavaScript, load-complete callback for single assets include SWF/Flash”

  1. Michael
    Mai 17th, 2010 @ 14:23

    Can I configure the swfJSPreLoader.swf path?

  2. Sebastian.Martens
    Mai 17th, 2010 @ 22:28

    Hi Michael,

    in deed i had the same issue this afternoon. I added an parameter _swfPath ( see code example above ).

    cheers,
    Sebastian

  3. keithics
    August 7th, 2010 @ 19:01

    I can’t seem to make it work. Any demo links?

  4. Sebastian.Martens
    August 9th, 2010 @ 22:52

    hi keithics,

    could you give me any osx / browser where it is not working so i could check it out ? Otherwise the complete source is available. There is also my test running.

    cheers,
    Sebastian

Leave a Reply