Flash CS3 Documentation |
|||
| Learning ActionScript 2.0 in Adobe Flash > 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:
For a sample source file that loads MP3 files, jukebox.fla, see the Flash Samples page at www.adobe.com/go/learn_fl_samples. Download the Samples zip file and navigate to the ComponentsAS2/Jukebox folder to access this sample. This sample demonstrates how to create a jukebox by using data types, general coding principles, and several components.
Flash CS3
Send me an e-mail when comments are added to this page | Comment Report
Current page: http://livedocs.adobe.com/flash/9.0/main/00001027.html