MovieClip.getBytesLoaded( ) Method | Flash 5 |
check the number of bytes that have downloaded to the Player |
An integer representing the number of bytes of mc 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 a movie or JPEG image that have downloaded into the Flash Player. However, data usually arrives in chunks of several hundred or several thousand bytes, and getBytesLoaded( ) measures bytes in terms of these chunks only. So, if a movie's first frame is 200 bytes in size and its second frame is 3000 bytes in size, getBytesLoaded( ) may return 200 and 3200, but it will never return any increment in between.
Note that internal movie clips are always loaded entirely before they are displayed, so the return value of getBytesLoaded( ) on an internal movie clip is always the same as getBytesTotal( ), unless the movie clip is currently loading an external file in response to loadMovie( ). Therefore, getBytesLoaded( ) is generally useful only when used with an external .swf or .jpg file being loaded into an instance or level.
The loadMovie( ) and unloadMovie( ) functions are executed asynchronously. Hence, the return value of getBytesLoaded( ) varies according to when it is called and the value of mc, as follows:
If mc is undefined, getBytesLoaded( ) returns undefined.
After a loadMovie( ) or unloadMovie( ) statement occurs in source code, but before it executes, and before any of the external file has been retrieved, getBytesLoaded( ) returns the size of the existing content in mc (prior to the loadMovie( ) or unloadMovie( ) statement). An empty movie clip created during authoring has a byte size of 4. An empty movie clip created via MovieClip.createEmptyMovieClip( ) has a byte size of 0.
Once bytes have been retrieved, getBytesLoaded( ) returns the number of bytes loaded so far, unless the file at url is not found.
If the file at url is not found, getBytesLoaded( ) returns 0 (even if mc is a document _level that did not previously exist). However, getBytesTotal( ) returns -1.
After an unloadMovie( ) command executes, getBytesLoaded( ) returns 0 if mc is a movie clip, or it returns undefined if mc is a document level, such as _level0. The same applies to getBytesTotal( ).
Like _framesloaded, getBytesLoaded( ) is normally used to build preloaders. It can be used in concert with getBytesTotal( ) to create a more accurate progress bar than is possible with _framesloaded and _totalframes (because the byte size of each frame may not be equal�a movie with 10 frames is not 30% loaded when three frames are loaded if the frames differ widely in size). See _framesloaded for example preloader code based on the number of downloaded frames.
Unfortunately, in Flash 6, the return value of getBytesLoaded( ) reflects the uncompressed size of Flash .swf files being loaded, not the size of the file after Flash compression has been applied. This means that getBytesLoaded( ) for a .swf file with a post-compression size of 2,000 bytes might return, say, 10,000 bytes.
The following code shows a simple preloader that displays download progress in two text fields:
// CODE ON FRAME 1 OF A MOVIE'S MAIN TIMELINE if (this._framesloaded > 0 && this._framesloaded = = this._totalframes) { this.gotoAndPlay("beginMovie"); } else { // Show the load progress in text fields. Divide by 1024 to convert bytes to KB. loadProgressOutput_txt.text = Math.floor(this.getBytesLoaded()/1024); loadTotalOutput_txt.text = Math.floor(this.getBytesTotal()/1024); } // CODE ON FRAME 2 this.gotoAndPlay(1);
loadMovie( ), MovieClip._framesloaded, MovieClip.getBytesTotal( ), MovieClip.loadMovie( ), MovieClip._totalframes, MovieClip.unloadMovie( ), unloadMovie( )