| How to Load an External MP3 Dynamically as a Sound Object using the loadSound Method | Return to Index | Do you want all FLA's used for this tutorial? | Do you have a question? |
The "loadSound" method of the sound object allows MP3 files to be loaded dynamically. Most of what has already been described about sound objects is true for dynamically loaded sound objects. The notable exceptions involve loading the sound object as streaming versus as an event. If the sound object is loaded as streaming, it begins to play before it has completely downloaded onto the user's computer. If the sound object is loaded as an event, the file must completely load before it can be played. The advantage of loading the audio as streaming is that it plays sooner. The advantage of loading the audio as an event is that it has, potentially, better sound quality.
|
The syntax for dynamically loading a sound object with an instance name of "myMusic" is as follows:
| myMusic.loadSound("url", isStreaming) |
Commonly, the sound object method, "loadSound", is used as follows:
| myMusic.loadSound("sample.mp3", true); |
In the above example, the MP3 file, sample.mp3, is loaded from the same folder that contains the SWF file. It loads into the sound object "myMusic", and it is loading as a streaming sound object. Concerning loading as streaming or as an event, loading as an event is the default. If neither true or false is specified for streaming, the sound object will load as an event.
To use the loadSound method, the sound object must still be defined. The main difference being that it is not attached to a sound in the library. The following is a common way to define the sound object with the loadSound method:
| myMusic = new Sound(myMusicMc); myMusic.loadSound("sample.mp3"); |
In the above example, a new sound object is defined with the instance name of "myMusic", and will load as a child of the movie clip with an instance name of "myMusicMc". This will give you the ability to control its properties independent of movie clip sounds on other timelines.
In the second line, the external file "sample.mp3" is instructed to load into the sound object "myMusic". It will load as an event, versus streaming, as the Boolean value of true or false is not specified. Therefore, the default status (event) will be used.
All of the controls for sound objects described thus far work for dynamically-loaded streaming MP3s, with the exception of starting the MP3 with the mySoundObject.start() method. Streaming MP3's start as soon as there is enough data to play the sound; therefore, the loadSound call to the sound object is the start command. However, you should be able to stop the streaming sound and restart it with the mySoundObject.start().
This document copyright © 2004 by Kenny Bellew of Cowfly.Com Design, kennybellew@hotmail.com