598 Working with Images, Sound, and Video
About loading and using external
MP3 files
To load MP3 files at runtime, use the loadSound() method of the Sound class. First, you
create a Sound object, as shown in the following example:
var song1_sound:Sound = new Sound();
Use the new object to call loadSound() to load an event or a streaming sound. Event sounds
are loaded completely before being played; streaming sounds play as they download. You can
set the
isStreaming parameter of loadSound() to specify a sound as a streaming or event
sound. After you load an event sound, you must call the
start() method of the Sound class
to make the sound play. Streaming sounds begin playing when sufficient data is loaded into
the SWF file; you don’t need to use
start().
For example, the following code creates a Sound object named
my_sound and then loads an
MP3 file named song1.mp3. Put the following ActionScript in Frame 1 on the Timeline:
var my_sound:Sound = new Sound();
my_sound.loadSound("http://www.helpexamples.com/flash/sound/song1.mp3",
true);
In most cases, set the isStreaming parameter to true, especially if you’re loading large sound
files that should start playing as soon as possible—for example, when creating an MP3
“jukebox” application. However, if you download shorter sound clips and need to play them
at a specified time (for example, when a user clicks a button), set
isStreaming to false.
To determine when a sound IS completely downloaded, use the
Sound.onLoad event
handler. This event handler automatically receives a Boolean value (
true or false) that
indicates whether the file downloaded successfully.
For more information, see the following topics:
■ “Loading an MP3 file” on page 599
■ “Preloading MP3 files” on page 599
■ “Reading ID3 tags in MP3 files” on page 601
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.