[ Team LiB ] |
Recipe 13.10 Adding Sounds to Buttons and UI Components13.10.1 ProblemYou want to add sounds that play when the user clicks a button or when a UI component selection is made. 13.10.2 SolutionAdd the sound to the movie by attaching it from the Library or by loading an external MP3. Create a Sound object to target the sound. Then add a call to start( ) within the button's or component's handler function. 13.10.3 DiscussionYou can use sounds in conjunction with buttons and UI components to alert the user that something has happened (i.e., a selection has been made, a button has been pressed, etc.). To accomplish this, you need to complete the following three steps:
You can use these techniques to play short sounds such as clicks or beeps when buttons are pressed or selections are made, as in the preceding snippets of code. Here is a complete example: // Attach a push button from the Library. You must first drag a push button from the // Components panel to the Stage to create the Library symbol. this.attachMovie("FPushButtonSymbol", "myPushButton", 1); // Create a sound holder movie clip. this.createEmptyMovieClip("soundHolder_mc", 2); // Create the Sound object. click_sound = new Sound(soundHolder_mc); // Load a sound from an external MP3. You can use this URL as long as you are playing // the movie in the test Player or standalone Player. click_sound.loadSound("http://www.person13.com/ascb/sounds/click.mp3"); // Define the click handler function. function onClick ( ) { click_sound.start( ); } // Set the click handler function and label for the push button. myPushButton.setClickHandler("onClick"); myPushButton.setLabel("Click!"); You can also use the same technique to play longer sounds. The following example shows how you can use a push button to toggle between playing and stopping an attached sound: // Attach a push button from the Library. You must first drag a push button from the // Components panel to the Stage to create the Library symbol. this.attachMovie("FPushButtonSymbol", "myPushButton", 1); // Create a sound holder movie clip. this.createEmptyMovieClip("soundHolder_mc", 2); // Create the Sound object. click_sound = new Sound(soundHolder_mc); // Attach the sound from the library. You must have a sound symbol with a linkage // identifier of MySoundSymbol for this to work. click_sound.attachSound("MySoundSymbol"); // Create two click handler functions: one to start the sound and one to stop the // sound. When one is called, it switches the push button's click handler and label // so that the sound alternately plays and stops. function startSound ( ) { click_sound.start( ); myPushButton.setClickHandler("stopSound"); myPushButton.setLabel("Stop Sound"); } function stopSound ( ) { click_sound.stop( ); myPushButton.setClickHandler("startSound"); myPushButton.setLabel("Start Sound"); } myPushButton.setClickHandler("startSound"); myPushButton.setLabel("Start Sound"); 13.10.4 See AlsoFor basic information on using sounds in Flash, see Help Using Flash Adding Sound. Also refer to Recipe 13.2, Recipe 13.3, and Recipe 15.5. |
[ Team LiB ] |