Enabling Sound with ActionScript Flash

Flash 5 offers many new object types, and one of the most exciting objects to use is the Sound Object. Like most objects, the Sound Object has predefined methods that you can use to control each new Sound Object. Table below provides an overview of the Sound Object and its methods. Reasons for using Sound Objects over traditional Sound Movie Clips or keyframe sounds:

  • Dynamic event sounds that play in a random or user-defined order.
  • Precise control over volume and panning.
  • The ability to dump (or erase) a Sound Object when the sound is no longer needed.

Unlike the Color Object that uses Movie Clips as targets, the Sound Object uses sounds directly from the movie’s Library. You cannot use the Sound Object to control sounds that are specified in the Sound Panel for any given keyframes. The next section shows you how to create Sound Objects, using the object constructor with the attachSound and start methods.

Methods for the sound object

Methods for the sound objectMethods for the sound object

Creating sound libraries with ActionScript
You learned how to create a sound library Movie Clip that stored several individual sound Movie Clip instances. You learned how to target these sounds in order to play them (or mute them). From a conceptual point of view, manually creating each sound Movie Clip enabled you to see each sound “object” on the Stage very easily. However, we can produce the sounds for a sound library much more quickly using ActionScript.

You can also open the pianoKeys_complete.fla file.

  1. Using the Open as Library command in the File menu, select the pianoKeys.fla file that you made previously. Opening a Flash file as a Library enables you to access symbols and media in that file.
  2. If you don’t have a new untitled Flash document open, then create a new Flash file (Ctrl+N or Command+N). Drag the soundLib Movie Clip from the pianoKeys Library onto the Stage of your new movie. If you open the Library for your new movie, you’ll see that all the elements contained within the soundLib Movie Clip have been imported into your new movie. Close the pianoKeys Library window, and save your new Flash movie as soundLib_ActionScript.fla.
  3. Select the soundLib instance on the Stage, and open the Instance Panel. Give the instance the name soundLib. Press the Return or Tab key to make the name “stick.”
  4. Double-click the soundLib instance on the Stage. In Symbol Editing Mode, create a new blank layer and delete all of the sound layers. (You always need to have at least one layer in a Movie Clip.) On the empty layer, draw an icon representing the soundLib Movie Clip. In this example, we made a white-filled rounded rectangle with soundLib black text. Center the icon elements to the Movie Clip Stage.
  5. Go back to the Main Timeline (click the Scene 1 tab in the upper right, or choose Edit➪Movie). Before we can attach sounds to the soundLib instance, each sound in the Library needs to be given a unique ID name in order for ActionScript to see it. Open the Library (Ctrl+L or Command+L), and select key_1.aif (or key_1.wav). Right-click (or Contrl+click on the Mac) the highlighted item, and choose Linkage in the contextual menu. In the Symbol Linkage Properties dialog, check the Export this symbol option and type sound_1 in the Identifier text field, as shown below. Click OK.
  6. Repeat the naming routine from Step 4 on each sound in the Library. Increase the number that you append to the end of sound_ for each new sound (for example, sound_2 for key_2.aif, sound_3 for key_3.aif, and so on).
  7. The attachSound method can only use sounds that have been set to export with the Flash .SWF file.

    attachSound method can only use sounds that have been set to export with the Flash .SWF file.

  8. Now, we need to add the ActionScript code that will create our Sound Object. We will construct a function that, when executed will form a list of sound instances. Create a new layer named actions and double-click its first keyframe. This will open the Actions Panel. With Expert Mode turned on, type the following code:
  9. These first lines establish the name of our function, createLib. We will want to dynamically change the number of sounds we create with this function. Therefore, we assign an optional parameter (called an argument) num that will be passed to the nested actions within the function. The second line starts a for loop that cycles its nested actions until the condition i<=num is no longer true. i starts (or initializes) with a value of 1, and the syntax i++ tells i to increase by 1 with each pass of the for loop. In the next step, we want the for loop to (a) create an array to store a reference to each sound instance; (b) create a new instance of the Sound Object for each sound in the Library; and (c) attach each sound in the Library to its new instance.

  10. In the Actions Panel, add the following ActionScript to the code from Step 6:
  11. The first line of code in Step 7 checks whether i’s current value is 1. During the first pass in the for loop, this will be true. So, the contents of the if nest will be executed. The second line of code occurs within the if nest. This line creates a new Array named snd and is made a property of this. this refers to the object that targets (or evokes) the createLib function. Because we’re only defining our function, we haven’t made this function a target for any Movie Clip instance. This line will only be executed once, while the value of i is 1. When i’s value increases in subsequent passes of the for loop, this line will be ignored.

    The third line executes a trace action, which sends alert messages to the Output window (in the Test Movie environment). The trace action in the third line will tell us that the actions in the if nest have been executed by sending new Array created to the Output window. The fourth line is also a trace action that tells us what object is evoking (or executing) the createLib function.

    The fifth line makes a new element in the snd array. The new element is a new Sound Object that is targeted at the this timeline. Ultimately, our Sound Objects will be tied to the soundLib Movie Clip instance, which you’ll see later. Each element in an array has a number indicating its position in the array. Because the value of i increases with each pass of the for loop, each Sound Object will have a unique position within the snd array.

    The sixth line uses the attachSound method to take a sound element in the Library and attach it to the Sound Object in the snd array. The target for the attachSound method is specified as “sound_” + i. On each pass of the for loop, this expression will return “sound_1”, “sound_2”, and so on until our limit prescribed by the num argument is reached. The complete block of code on the first keyframe of the actions layer should look like this:

  12. Now that we have a function defined to create all the Sound Objects on a this Object (or timeline), we need to have an object (for this to refer to) that uses the createLib function. In the Actions list for frame 1 of the Actions layer, type the following code after the function createLib:
  13. The first line of code defines a method called createLib that used the function createLib as a value. Because createLib is a function, the createLib method of soundLib will execute the createLib function whenever the method is evoked.

    The second line of code evokes the createLib method—the use of () after the method name indicates that the method is being executed, not defined. In addition to executing the createLib method, we’re also sending the function the number 7 as the num argument. Therefore, seven Sound Objects will be created.

  14. Save the Flash movie file and test it (Ctrl+Enter or Command+Enter). The Output window should open and display the trace statements:
  15. Close the Test Movie window and return to the authoring environment. Double-click frame 1 of the actions layer, and add this last bit of code to the
  16. The first line of code targets the first declared element, 1, of the snd array, and tells it to begin playback with the start method. Remember that element 1 in the array is a Sound Object, which references the sound_1 ID in the Library. The second line of code targets the second declared element, 2, of the snd array, and tells it to start.

  17. Save the Flash movie and test it. Both lines of code will execute simultaneously.
  18. So, you will hear sound_1 (which is key_1.aif or key_1.wav) and sound_2 (key_2. aif or key_2.wav) play together. Now you should practice targeting these Sound Objects with Button instances and other keyframes. To access a different sound, simply change the number in the array brackets. The next tutorial introduces the pan methods of the Sound Object, and shows you how to use Sound Objects with interactive projects.

Creating a soundTransformObject
The two remaining methods of the Sound Object, setTransform and getTransform, work in the same manner as the transform methods of the Color Object. You need to create a generic object using the object constructor before the setTransform method can be used with a Sound Object. This generic object will become a sound TransformObject once we have assigned sound channel properties to the generic Object.

Luckily, the soundTransformObject doesn’t have as many properties as the colorTransformObject, and they’re much simpler to predict with trial and error testing. The properties of the soundTransformObject are:

  • ll, the percentage of left channel output in the left speaker
  • lr, the percentage of right channel output in the left speaker
  • rr, the percentage of right channel output in the right speaker
  • rl, the percentage of left channel output in the right speaker

The first letter of each property determines which physical speaker is being affected. The second letter determines which channel’s output (or its volume) is played in that speaker. Each property can have a value between –100 and 100. The steps to produce and incorporate a soundTransformObject are nearly the same as the colorTransformObject. The only difference is that you specify paths to Sound Objects rather than Movie Clip Objects for the setTransform and get Transform methods.



Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Flash Topics