About loading and using external MP3 files 599
Loading an MP3 file
suppose you’re creating an online game that uses different sounds that depend on what level
the user has reached in the game. The following code loads an MP3 file (song2.mp3) into the
game_sound Sound object and plays the sound when it IS completely downloaded.
To load an MP3 file:
1. Create a new FLA file called loadMP3.fla.
2. Select Frame 1 on the Timeline, and then type the following code in the Actions panel:
var game_sound:Sound = new Sound();
game_sound.onLoad = function(success:Boolean):Void {
if (success) {
trace("Sound Loaded");
game_sound.start();
}
};
game_sound.loadSound("http://www.helpexamples.com/flash/sound/song2.mp3"
false);‘
3.
Select Control > Test Movie to test the sound.
Flash Player supports only the MP3 sound file type for loading sound files at runtime.
For more information, see
Sound.loadSound(), Sound.start(), and Sound.onLoad in the
ActionScript 2.0 Language Reference. For information on preloading MP3 files, see “Preloading
MP3 files” on page 599. For information on creating a progress bar animation when you load
a sound file, see “Creating a progress bar for loading MP3 files with ActionScript”
on page 627.
You can find a sample source file that loads MP3 files, jukebox.fla, in the Samples folder on
your hard disk. This sample demonstrates how to create a jukebox by using data types, general
coding principles, and several components.
■ In Windows, browse to boot drive\Program Files\Macromedia\Flash 8\Samples and
Tutorials\Samples\Components\Jukebox.
■ On the Macintosh, browse to Macintosh HD/Applications/Macromedia Flash 8/Samples
and Tutorials/Samples/Components/Jukebox.
Preloading MP3 files
When you preload MP3 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 downloading progress of MP3 files, use the
Sound.getBytesLoaded()
and
Sound.getBytesTotal() methods.