First time here? First time here? Newsletter Signup Newsletter
651326 Members and growing! | Flash Jobs
Search tutorials
Author: Kenny Bellew

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.

Fig. 17: Example of loading an external MP3 file

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 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.


Fig. 17b: Testing the Flash player for compatibility with mySoundObject.start()

» Level Intermediate

Added: 2002-08-13
Rating: 8.98 Votes: 664
(10 being the highest)
» Author
Kenny Bellew is a technical writer in Minneapolis, MN. He's currently interested in becoming involved in other writing-related Flash projects.
» Download
Download the files used in this tutorial.
» Forums
More help? Search our boards for quick answers!

Comments

  • There are no comments yet. Be the first to comment!

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.
Featured Flash FLA
» Author: doc_chin7
» Description:

The flash's horizontal road drive was done in CS4 and was upgraded to CS6. The car responds to right/ left key and flash will auto-generate the movement of roads from left-right or right-left based on random number. Have fun driving in this virtual environment and avoid other cars... :) If you are creative, a road driving game can be created from it.

Featured Sound Loops
Image for equalize

Audio Player

» Author: BoxCat
» Title: CPU Talk
» Description: From the cyberpunk RPG, "Nameless: the Hackers" Completely Free to use! Keywords: edm hip hop rap electronic mysterious mission hacking chase exciting spy ios ambient chilling love happy sad angry suspense
Latest Font
» Author: Fábio FAFERS
» Description: I created this font for free use. Everyone can apply it in personal or business texts. Its free, but I want to be communicated in case of business use. Donations are accepted to keep the project of free fonts alive! Thank you all
Featured Sound Fx
Image for equalize

Audio Player

» Author: nathan
» Description:

A recorded sound of the Dimensional Fork Gate.