User Guide
302 Chapter 12: Working with External Media
To display download progress using the ProgressBar component:
1.
In a new Flash document, create a movie clip on the Stage and give it an instance name
target_mc.
2.
Open the Components panel (Window > Development Panels > Components).
3.
Drag a ProgressBar component from the Components panel to the Stage.
4.
In the Property inspector, give the ProgressBar component the name pBar and, on the
Parameters tab, select Manual from the Mode pop-up menu.
5.
Select Frame 1 in the Timeline, and open the Actions panel (Window > Development Panels >
Actions).
6.
Add the following code to the Actions panel:
// create both a MovieClipLoader object and a listener object
myLoader = new MovieClipLoader();
myListener = new Object();
// add the MovieClipLoader callbacks to your listener object
myListener.onLoadStart = function(clip) {
// this event is triggered once, when the load starts
pBar.label = "Now loading: " + clip;
};
myListener.onLoadProgress = function(clip, bytesLoaded, bytesTotal) {
var percentLoaded = int (100*(bytesLoaded/bytesTotal));
pBar.setProgress(bytesLoaded, bytesTotal);
};
myLoader.addListener(myListener);
myLoader.loadClip("veryLargeFile.swf", target_mc);
7.
Test the document by selecting Control > Test Movie. You can see the movie load.
8.
Publish to HTML, and open the HTML file in a browser to see the progress bar in action.
For more information, see “MovieClipLoader class” in Flash ActionScript Language Reference.
Preloading MP3 and FLV files
To preload MP3 and FLV files, you can use the
setInterval() function to create a polling
mechanism that checks the bytes loaded for a Sound or NetStream object at predetermined
intervals. To track the download progress of MP3 files, use the
Sound.getBytesLoaded() and
Sound.getBytesTotal() methods; to track the download progress of FLV files, use the
NetStream.bytesLoaded and NetStream.bytesTotal properties.
The following code uses
setInterval() to check the bytes loaded for a Sound or NetStream
object at predetermined intervals:
// Create a new Sound object to play the sound.
var songTrack:Sound = new Sound();
// Create the polling function that tracks download progress.
// This is the function that is "polled." It checks
// the download progress of the Sound object passed as a reference.
checkProgress = function (soundObj) {
var bytesLoaded = soundObj.getBytesLoaded();
var bytesTotal = soundObj.getBytesTotal();
var percentLoaded = Math.floor(bytesLoaded/bytesTotal * 100);