Playing External Sounds

 < Day Day Up > 

You can play MP3 sounds that reside outside a movie. This means your movie can stay nice and small and load sounds only as the user requests them. It also means you can swap out and replace the original MP3 files without needing to reimport them into Flash. There are a few subtleties on how it works, but the script is easy enough to learn. The next task shows you how to make a jukebox application.

Try It Yourself: Play an External MP3

In this task you'll build a mini jukebox from which users can select songs to play. Follow these steps:

1.

Find a few MP3 files and place them in a new folder. Name the files song1.mp3, song2.mp3, and song3.mp3.

2.

Drag a List component onto the Stage and give it an instance name songs_lb. Use the Free Transform tool to make the list nice and wide.

3.

Put this script in the first keyframe of the movie:

 1 songs_lb.addItem("Title of song one", "song1.mp3"); 2 songs_lb.addItem("Title of song two", "song2.mp3"); 3 songs_lb.addItem("Title of song three", "song3.mp3"); 4 5 songs_lb.addEventListener("change",playSong); 6 function playSong() { 7    filename=songs_lb.selectedItem.data; 8    my_sound=new Sound(); 9    my_sound.loadSound(filename,true); 10 } 

4.

Naturally, you can replace the titles for the songs in Lines 1 3. Notice that the second parameter in each of Lines 1 3 is the actual filename. These filenames will be hidden in the List item's data properties. Every time the user selects a song, the homemade playSong() function triggers. Line 7 sets a homemade variable filename to the data in the currently selected item. Finally, Lines 8 and 9 are really all you need for an external sound. The first one makes a Sound object instance (stored in the homemade my_sound variable). Then, Line 9 loads the sound. The second parameter (true for streaming) makes the sound begin playing as soon as enough has downloaded (instead of waiting for it to entirely download).

Similarly to how Flash only loads images in the .jpg file format, the loadSound action only plays files in the .mp3 format.

By the Way: Media Components in Flash Professional 8

Just as the Loader and ProgressBar components made loading external .swf files a snap, Flash Pro comes with three special media components. They include everything you need to easily load, play, and even let the user pause or resume external sounds and videos. Of course you can do all this by hand, but these components make it very easy.


     < Day Day Up > 


    Sams Teach Yourself Macromedia Flash 8 in 24 Hours
    Sams Teach Yourself Macromedia Flash 8 in 24 Hours
    ISBN: 0672327546
    EAN: 2147483647
    Year: 2006
    Pages: 235

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