Loading MP3 Audio into Flash Movies


If you thought image loading was exciting, wait until you see the support for MP3 audio loading into Flash Player 6 or higher movies. You can load MP3 files directly into Sound objects that you create with ActionScript.

Cross-Reference 

If you don't know how to use Sound objects, read our coverage of Sound objects in Chapter 27, "Interacting with Movie Clips." You may also want to read Chapter 15, "Adding Sound," if you are unfamiliar with general sound use in Flash movies. You can find expanded and more advanced coverage of Sound objects in the Flash ActionScript Bible series (Wiley).

Here are a few tips for using MP3 files that will be loaded into Flash Player 6 or higher movies:

  • Watch file size. MP3 files, especially those of full-length songs, can be very large in file size, easily exceeding 3 MB. Make sure your target audience can accommodate the file sizes you are loading.

  • Test your specific MP3 encoding method(s) before you use the same encoding on several files that you intend to use with Flash Player 6 or higher movies. In our tests, we have not encountered problems with any MP3 files that we loaded into the Flash Player. However, there are several variations of CBR (Constant Bit Rate) and VBR (Variable Bit Rate) encoding methods available in several audio applications.

  • Depending on the Flash Player release version, MP3 files may not cache on the user's hard drive. All MP3 files loaded through the Sound object in ActionScript are stored in the computer's virtual memory. Be careful with the number of MP3 files downloaded and stored in memory simultaneously.

Cross-Reference 

Review the troubleshooting tips we listed for loadMovie() and images in the last section. The same principles apply to MP3 files.

The core method to the Sound object that can access external MP3 files is the loadSound() method. With this method, you can specify a path to an MP3 file just like you did for JPEG images with the loadMovie() method. However, the loadSound() method has an additional argument: isStreaming. We'll discuss this in a moment. The syntax for using the loadSound() method is:

 soundObject.loadSound(URL, isStreaming); 

soundObject indicates the name of a Sound object created previously in ActionScript and URL is the relative or absolute path to the MP3 file. isStreaming is a Boolean value (true or false) that determines whether the MP3 file automatically begins playback as soon as enough bytes have loaded into the Flash Player (true) or whether the MP3 file must fully download before playback can begin (false).

On the CD-ROM 

Before you begin the following exercise, make a copy of the load_image_100.fla file located in the ch28 folder of this book's CD-ROM. You will modify the existing structure of the document that you built in earlier sections of this chapter. You will also need an MP3 file copied to the same location on your hard drive. You can use the atmospheres_1.mp3 file located in the ch28 folder as well.

In the following steps, you learn how to load an MP3 file into a Sound object.

  1. Open the load_image_100.fla document. Resave this document as loadSound_100.fla.

  2. Select the Load Image Button component instance on the Stage. In the Parameter tab of the Property inspector, change the label value to Load MP3.

  3. Select the Unload Image Button component instance on the Stage. In the Property inspector, change the label value to Unload MP3.

  4. Select the Input text field on the Stage and change the instance's name to tSnd in the Property inspector.

  5. Now you need to change the ActionScript in the document to use the Sound object and the loadSound() method. Select frame 1 of the actions layer and open the Actions panel (F9, or Option+F9). Change the code in the Script pane to match the code shown in Listing 28-6.

    Listing 28-6: Using loadSound() for External MP3 Files

    image from book
     1.  var cbtLoad:mx.controls.Button; 2.  var cbtUnload:mx.controls.Button; 3.  var tSnd:TextField; 4.  var mcHolder:MovieClip; 5.  var snd:Sound; 6. 7.  var oLoader:Object = new Object(); 8.  oLoader.click = function(oEvent:Object):Void { 9.     var sLabel:String = oEvent.target.label.toLowerCase(); 10.    if(sLabel == "load mp3"){ 11.       snd = new Sound(); 12.       snd.loadSound(tSnd.text, true); 13.    } else if (sLabel == "unload mp3"){ 14.       snd.stop(); 15.       delete snd; 16.    } 17. }; 18. 19. cbtLoad.addEventListener("click", oLoader); 20. cbtUnload.addEventListener("click", oLoader); 
    image from book

    In lines 1 through 5, you declare the variable names that will be used throughout the script. Here, you add a Sound variable named snd in line 5.

    In lines 8 through 17, the click() method of the oLoader listener is invoked by the cbtLoad and cbtUnload instances. If the cbtLoad instance is clicked, lines 10 through 12 are invoked. A new Sound object named snd is created (line 11). Then, in line 12, the loadSound() method of the object is executed, using the URL specified in the tSnd field. The isStreaming argument is set to true so that the MP3 automatically begins to play as soon as enough of the sound file has downloaded into the movie.

    If the cbtUnload instance is clicked, lines 13 through 15 are invoked. In line 14, the stop() method of the Sound class halts playback of the sound, and in line 15, the snd object is removed from the movie.

  6. Save your Flash document, and test it (Ctrl+Enter or z+Return). In the Test Movie window, type atmospheres_1.mp3 into the sound_txt field. Click the Load MP3 button, and the MP3 begins to play. When you click the Unload MP3 button, the sound stops playing.

Try using other URLs of MP3 files, either locally or remotely. You can specify the following URL to stream the same MP3 file from our Web server:

  • http://www.theMakers.com/sounds/atmospheres_1.mp3

Note 

It may take a few moments for remote sound files to load into Flash Player.

You can continue modifying the Sound object into which the MP3 file loads. For example, you can apply the same volume and balance output control from the examples in Chapter 27, "Interacting with Movie Clips," to the Sound object in this Flash movie.

We will show you how to monitor the download progress of an MP3 file (and other external files) later in this chapter.

On the CD-ROM 

You can see the completed Flash document, loadSound_100.fla, in the ch28 folder of this book's CD-ROM.




Macromedia Flash 8 Bible
Macromedia Flash8 Bible
ISBN: 0471746762
EAN: 2147483647
Year: 2006
Pages: 395

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net