• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint

Playing External Sounds

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:

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

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.

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");
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 }

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



Not a subscriber?

Start A Free Trial

  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint