![]() ![]() ![]() | |
![]() | |
![]() | |
![]() |
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 possiblefor 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.
For example, suppose you're creating an online game that uses different sounds depending on what level the user has reached in the game. The following code loads an MP3 file (blastoff.mp3) into a Sound object named gameSound
, and then plays the sound when it has completely downloaded:
var gameSound = new Sound(); gameSound.onLoad = function (loadedOK) { if(loadedOK) { gameSound.start(); } } gameSound.loadSound("http://server.com/sounds/blastoff.mp3", false);
For sound files, Flash Player supports only the MP3 sound file type.
For more information, see Sound.loadSound()
, Sound.start()
, and Sound.onLoad
.
![]() | |
![]() | |
![]() | |
![]() ![]() ![]() |