Sound.getBytesLoaded( ) Method | Flash 6 |
the number of downloaded bytes of an external sound file |
An integer representing the number of bytes that have finished downloading to the Player. Divide by 1024 to convert bytes to kilobytes (KB).
The getBytesLoaded( ) method tells us the number of bytes of an external sound file that have downloaded into the Flash Player due to a loadSound( ) call. For a new Sound object, getBytesLoaded( ) returns undefined. Immediately after each call to loadSound( ), getBytesLoaded( ) also returns undefined. As data arrives, getBytesLoaded( ) returns the number of bytes transferred so far (from 0 to getBytesTotal( )), until the entire sound has loaded. From then on, it returns the total number of bytes last transferred, until the next call to loadSound( ).
For Sound objects, getBytesLoaded( ) is used with getBytesTotal( ) to determine (and display, normally) the load progress of a downloading sound file. However, merely detecting the completion of a load operation does not require getBytesLoaded( ); the onLoad( ) event handler is executed automatically when a sound finishes loading.
The following code adds a new method, preloadSound( ), to all Sound objects. It operates in the same way as loadSound( ), except that it uses getBytesLoaded( ) to report load progress while the sound is loading. The onLoad( ) handler responds to completion of the sound load operation, as usual. Note that as is, the code cannot load multiple sounds simultaneously because it does not queue preload requests. A queuing preloader is left as an exercise for the reader.
/* * Method: Sound.checkLoadProgress() * Desc: Checks progress of a load(). Called only by preload(). * Reports progress by invoking a custom Sound.onBytesLoaded() handler. */ Sound.prototype.checkLoadProgress = function () { // Check how many KB have loaded. var kbLoaded = Math.floor(this.getBytesLoaded()/1024); // Check the size of the file loading, in KB. var kbTotal = Math.floor(this.getBytesTotal()/1024); // Calculate the percent complete. var percentDone = isNaN(Math.floor(kbLoaded/kbTotal * 100)) ? 0 : Math.floor(kbLoaded/kbTotal * 100); // Execute onBytesLoaded(), which typically would display load progress. this.onBytesLoaded(this.getBytesLoaded(), this.getBytesTotal(), kbLoaded, kbTotal, percentDone); // If the amount loaded is equal to the total file size, // the file is finished loading. However, we must ensure // that getBytesLoaded() returns a value greater than 0 // because both getBytesLoaded() and getBytesTotal() // return 0 until the first bit of data arrives. if (this.getBytesLoaded() > 5 && this.getBytesLoaded() = = this.getBytesTotal()) { // ...stop calling checkLoadProgress(). this.clearLoadCheck(); } } /* * Method: Sound.clearLoadCheck() * Desc: Cancels an interval calling checkLoadProgress(). */ Sound.prototype.clearLoadCheck = function () { // If there is a load interval for this Sound object... if (this.loaderID) { // ...stop calling it. clearInterval(this.loaderID); } } /* * Method: Sound.preloadSound() * Desc: Begins the download of a sound file and executes * checkLoadProgress() every 200 ms until the file has loaded. */ Sound.prototype.preloadSound = function (url, isStreaming) { // Always stop any previous preload before proceeding. this.clearLoadCheck(); // Load the Sound file. this.loadSound(url, isStreaming); // Call checkLoadProgress() every 200 milliseconds. this.loaderID = setInterval(this, "checkLoadProgress", 200); } // Usage: // Create a Sound object. music = new Sound(); // Assign an onLoad() callback. music.onLoad = function (success) { // If the load was successful... if (success) { // ...use the new sound. trace("done loading!"); this.start(); } else { // ...otherwise, perform failure operations and // cancel the preload interval (REQUIRED!). trace("load failed"); this.clearLoadCheck(); } } // Assign an onBytesLoaded() callback. This is where each individual // Sound object can decide what to do with the preload information. music.onBytesLoaded = function (bytesLoaded, bytesTotal, kbLoaded, kbTotal, percentLoaded) { trace("Loading: " + kbLoaded + " of " + kbTotal); trace(percentLoaded + " percent complete."); } // Load a streaming sound. music.preloadSound("http://www.yoursite.com/sounds/song.mp3", true);
setInterval( ), Sound.getBytesTotal( ), Sound.onLoad( )