How to Load an External MP3 Dynamically as a Sound Object
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 syntax for dynamically loading a sound object with an instance name of "myMusic" is as follows:
Commonly, the sound object method, "loadSound", is used as follows:
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);
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 MP3's, 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() call. Currently, this option is only partially available due to a bug in the Flash Player.
With the r40 release of version 6 Flash player, this issue was addressed. If you are going to use the mySoundObject.start() method to restart the streaming sound object, you must make sure that the user has the latest Flash player. The Flash Deployment Kit from Macromedia provides instructions on making sure the latest plug-in is installed on the user's computer.
Test your Flash player for this issue with the Flash example in Fig. 17b.
|» Level Intermediate|
Rating: 8.98 Votes: 664
|Kenny Bellew is a technical writer in Minneapolis, MN. He's currently interested in becoming involved in other writing-related Flash projects.|
|Download the files used in this tutorial.|
|Download (6170 kb)|
|More help? Search our boards for quick answers!|