Loading external MP3 files 195
About loaded SWF files and the root Timeline
The ActionScript property
_root specifies or returns a reference to the root Timeline of a SWF
file. If you load a SWF file into a movie clip in another SWF file, any references to
_root in the
loaded SWF file resolve to the root Timeline in the host SWF file, not that of the loaded SWF
file. This can sometimes lead to unexpected behavior at runtime, for example, if the host SWF file
and the loaded SWF file both use
_root to specify a variable.
In Flash Player 7 and later, you can use the
MovieClip._lockroot property to force references to
_root made by a movie clip to resolve to its own Timeline, rather than to the Timeline of the
SWF file that contains that movie clip. For more information, see “Specifying a root Timeline for
loaded SWF files” on page 123.
About accessing data in loaded SWF files
One SWF file can load another SWF file from any location on the Internet. However, for one
SWF file to access data (variables, methods, and so forth) defined in the other SWF file, the two
files must originate from the same domain. In Flash Player 7 and later, cross-domain scripting is
prohibited unless the loaded SWF file specifies otherwise by calling
System.security.allowDomain().
For more information, see “Flash Player security features” on page 188 and
System.security.allowDomain() in Chapter 12, “ActionScript Dictionary,” on page 205.
Loading external MP3 files
To load MP3 files at runtime, use the loadSound() method of the Sound class. First, create a
Sound object:
var song_1_sound = new Sound();
You then 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 are played as they are
downloaded. You can set the
isStreaming parameter of loadSound() to specify a sound as an
event sound or a streaming 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
classical and then loads an
MP3 file named beethoven.mp3:
var classical:Sound = new Sound();
classical.loadSound("http://server.com/mp3s/beethoven.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’re downloading 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 has completely downloaded, use the Sound.onLoad event handler.
This event handler automatically receives a Boolean (
true or false) value that indicates whether
the file downloaded successfully.