The three major types of sounds for use in Flash movies include music, sound effects, and narration. Learn how to add sound to Flash movies, how to use media components and work with media classes (from Macromedia Flash MX Professional 2004 by Bill Sanders, 2004, Sams, ISBN 0672326051).
Adding Video and Sound - Project: Experimenting with Sound (Page 6 of 9 )
To show you how several Sound class features can be employed, the following application uses UI component buttons to start and stop a sound and sliders to control the volume and pan. A single MP3 file named jazz1.mp3 is employed, but any MP3 file can be used in its place. The following steps show how to create the application:
In a new Flash document, create six layers named, from top to bottom, Actions, Labels, Buttons, Levers, Grooves, and Background. Provide a gray background color (#A6A6A6).
Using Figure 10.5 as a guide, in the Grooves layer draw a vertical line with settings H=100, X=100, Y=140, and a horizontal line with settings W=200, X=200, Y=200 using color code #A699E6. Then in the Background layer, add solid black rectangles. In the
Buttons layer, drag two UI component buttons to the stage, providing the labels Start Stream to the top button and Stop Sound to the bottom button. Add the instance name stream_btn to the top button and stop_btn to the bottom button.
In the Labels layers, add static text labels as shown in Figure 10.5. Also, in the Labels layer centered beneath the Pan label, add a dynamic text field and provide the instance name soundStatus_txt.
Figure 10.5 Sound controllers.
Select the Levers layer and draw a rectangle with the dimensions W=31, H=8. Select the rectangle and press F8 to convert it into a movie clip with the name lever. Give it the instance name vol_mc. From the Library panel, drag a second instance of the movie clip. Rotate the second lever 90 degrees, and give it the instance name pan_mc. Place the vol_mc instance at the top of the vertical groove line and place pan_mc in the middle of the horizontal groove line, as shown in Figure 10.5.
Click the first frame of the Actions layer. Open the Actions panel and add the following script:
//Instantiate sound instance var soundHere:Sound = new Sound(); //Load a streaming sound stream_btn.onPress = function() { soundHere.loadSound("jazz1.mp3", true); }; soundHere.onLoad = function() { soundStatus_txt.text = "Sound streaming"; }; //Stop the streaming sound stop_btn.onPress = function() { soundHere.stop(); soundStatus_txt.text = "Sound stopped"; }; //Volume Control vol_mc.onPress = function() { this.startDrag(false, 115, 140, 115, 240); }; vol_mc.onRelease = function() { stopDrag(); }; var hearVol:Object = new Object(); hearVol.onMouseMove = function() { soundHere.setVolume(240-vol_mc._y); }; Mouse.addListener(hearVol); //Pan Control pan_mc.onPress = function() { this.startDrag(false, 195, 184.5, 395, 184.5); }; pan_mc.onRelease = function() { stopDrag(); }; var hearPan:Object = new Object(); hearPan.onMouseMove = function() { soundHere.setPan(295-pan_mc._x); }; Mouse.addListener(hearPan); //Signal end of sound soundHere.onSoundComplete = function() { soundStatus_txt.text = "Sound finished"; };
The volume slider control is set to generate values from 0 to 100, and the pan slider control is set to generate values from –100 to 100. The two buttons invoke methods to begin the progressive download (streaming) and to stop the sound instance. The onLoad() event handler indicates that the progressive download has "loaded" as soon as enough of the file has entered the buffer to begin playing. Therefore, only a small part of a stream sound needs to be loaded to fire the onLoad() event handler, whereas an event sound must be fully loaded to invoke the same event.
In tests with the sample file (a 940KB MP3 music file), the buffering time ranged from 3 to 7 seconds on high-speed connections (cable and DSL). The longer test periods were on DSL lines between the Eastern U.S. and Europe, whereas the shorter time was to and from an East Coast U.S. city. With slower Internet speeds, the buffering time could increase significantly, and a "buffering" message lets the user know that something is happening while he or she is waiting for the file to begin playing.
A new feature added to the Sound class is the ID3 property and event handler. ID3 information is optionally added to an MP3 file, but if the information is available, you can incorporate it into your document. The read-only information in the ID3 property can be used in conjunction with the onID3 event handler in the following format:
With 47 different ID3 properties (tags) available, from album name to the name and the length of the song, you can find a good deal of information you may want to incorporate into your Flash document. Figure 10.6 shows a partial list of the ID3 properties you can select from the Sound class pop-up menu.
Figure 10.6 A pop-up menu shows the different ID3 properties.
Many MP3 files have minimal or no ID3 information at all. When no ID3 information is detected using the onID3 event handler, you will either get an "undefined" response, indicating that some ID3 information is in the MP3 file but not the property you requested, or an "empty" ID3 response, indicating that no ID3 information at all is available in the file.
This chapter is from Macromedia Flash MX Professional 2004, by Bill Sanders (Sams, 2004, ISBN: 0672326051). Check it out at your favorite bookstore today.