Loading MP3s Dynamically
MP3s are everywhere these daysa popularity that can be attributed to the fact that they provide an acceptable way of delivering audio (especially music) over the Web in a low-bandwidth world. Although MP3s are much more compact than standard, noncompressed audio files, a single MP3 of any length (let alone several MP3s) can still balloon a movie's file size to an unacceptable level.Flash helps to alleviate this problem by providing the capability to dynamically load MP3 files into movies. As you'll soon see, loading an external MP3 into our project is a bit different from loading an external movie or JPGbut almost as easy.If you want to load an MP3 into Flash, the first thing you need to do is create a place where you can load the file. This requires a Sound object:
After you've created a Sound object, simply use the loadSound() method to load an external MP3 into that object:
var myFavMP3:Sound = new Sound();
This script loads the external MP3 file named mySong.mp3 into the myFavMP3 Sound object. After the MP3 has been loaded, you can control volume, panning, and other characteristics (see Lesson 17, "Scripting for Sound"). Notice that the loadSound() method has two parameters. The first parameter identifies the path to the external MP3; the other parameter determines whether the loaded file will be streamed (true) or considered an event sound (false). If this parameter is set to true, the externally loaded file begins playing as soon as a sufficient amount of the file has been downloaded. If set to false, the start() method must be invoked before the file will play:
myFavMP3.loadSound("mySong.mp3", true);
myFavMP3.start();
NOTEIf the file is loaded as an event sound, it won't playeven if you invoke the start() methoduntil the complete file has been downloaded.In this exercise, we'll make it possible for each loaded image in the slideshow to have an associated music track that's loaded externally.
Open virtualaquarium6.fla.We'll simply add some script to Frame 1 of this file from the preceding exercise.Before going any further, let's review the contents of the directory that holds our externally loaded assets.Navigate to the Lesson18/Assets directory and locate the files music0.mp3, music1.mp3, and music2.mp3.Return to Flash. With the Actions panel open, select Frame 1 of the Actions layer and modify the slides array:
slides = new Array(["Shark", "image0.jpg", "music0.mp3"], ["Jellyfish", "image1.jpg",Here, the path/name of an MP3 file has been added to each element in the array.Next, we'll alter the changeSlide() function defined in an earlier exercise, to make use of this new data.Insert the following line of script above the changeSlide() function definition:"music1.mp3"], ["Seahorse", "image2.jpg", "music2.mp3"]);
In the next step, we'll add script to the changeSlide() function that makes use of this Sound object. As we've mentioned before, the Sound object is created outside the function because it needs to exist beyond the execution of the function.Add the following lines of script at the end of (but within) the if statement of the changeSlide() function definition:
var slideSound:Sound = new Sound();
slideSound.stop();
slideSound = new Sound();
slideSound.loadSound(slides[number][2], true);
