| How to Build a Preloader for Dynamically Loaded MP3's | Return to Index | Do you want all FLA's used for this tutorial? | Do you have a question? |
When building a Flash movie that will contain MP3's, often the MP3's are dynamically loaded (versus being embedded in the SWF) to keep the initial Flash movie size smaller. Once the decision is made to load the MP3's dynamically, it becomes a decision between loading the sound as an event or as a streaming sound. Event sounds sometimes sound better than streaming sounds, as streaming sounds can omit samples of the sound within the stream in order to play the sound as soon as possible.
|
For example, if you opt to play a large-file-sized sound as an event associated with a button press and do not include a preloader, the user will be confused. He or she will press the button and it will appear that nothing is happening. Actually, the sound is loading in the background.
The preloader described here will work for event and streaming MP3's. However, it is not logical to build a preloader for a streaming sound, as it will start playing before the preloader reaches 100 percent.
Preloading a dynamically loaded MP3 sound object is very similar to creating a preloader for an entire movie. The sound object method's include methods for getting the total number of bytes contained in the file and comparing this against the number of bytes being loaded. These methods are soundObject.getBytesLoaded() and soundObject.getBytesTotal(). They are commonly used as follows:
| mySoundObject.getBytesTotal(); mySoundObject.getBytesLoaded(); |
One important thing to keep in mind when using these methods is that the information these methods report back is not available until the sound actually begins to load using the mySoundObject.loadSound method. Even though the sound object has been defined, the sound must start to load before the information is accessible. If you have ever seen a preloader reporting "NaN" (Not a Number), it may be because the ActionScript is attempting to calculate data that is not yet available.
The preloader approach presented here will use a load bar that will increase in size (scale) along the x-axis corresponding to the percentage of the file that has loaded. A push button will be used to begin loading the sound.
![]() |
|
| on(press) { if (playing!=true) { playing=true; preloadNow=1; mySound.loadSound("sample.mp3"); } mySound.onSoundComplete = function() {playing=false} }// |
In the above example, two variables are set. The variable "playing" is set to true to keep the file from loading multiple copies of the MP3 file - one for each press. Of course, this works because the first if-statement basically asks, "If playing does not equal true, then do the following". The playing variable is reset to "false" when the sound completes, allowing the user to press the play button again. You could add an additional conditional test to see if the sound had already been loaded once, which would then only start the sound. However, this is a judgment call. Because the sound is cached on the user system, the sound will load much more quickly.
The call to start the sound is not included in the above button code. It will be added to a conditional statement which allows the sound to start once it is fully loaded.
this.onLoad = function () { |
In the above example, the x-axis scale of the load bar (_xscale) is set to zero on initial load, causing it to be completely compressed at start. The variables mySoundBytesTotal and mySoundBytesLoaded are set to equal the the total bytes of the loading sound object "mySound". When the user presses the load-sound button, "preloadNow" will equal 1 and the sound will start to load. When "preloadNow" equals 1 on button press and mySoundBytesLoaded is greater than zero, the load bar's _xscale will begin to equal the growing percentage of the loading MP3. Once the percentage reaches 100, "preloadNow" is reset to zero, which will stop the code from continuing to run unnecessarily. The sound object is instructed to start once the percentage loaded equals 100. While all this is happening, the dynamic text box is continually updated to reflect the percentage loaded.
If you wanted to preload one sound and have it play as soon as it loads, you could also use the onLoad handler. Here is an example using the onLoad handler.
| Fig. 21b: Using the onLoad handler to start play. |
For the above player, all of the code to define, load, preload and play the sound is placed in the Action pane (press F9 to see actions pane) an empty movie clip with an instance name of myMusicMc. Here is the script that was used:
onClipEvent(load) { // Update text box with percent loaded // Scales load bar // |
This document copyright © 2004 by Kenny Bellew of Cowfly.Com Design, kennybellew@hotmail.com