|
|
||||
|
|
Ultrashock Tutorials > Flash5 > Sound Object - Part I | |||
|
||||
|
|
Sound Object - Part I Basics |
|
||
|
Sounds Good, Man! One of the new exciting features of Flash 5 is the sound object. Flash has long allowed us to add sound through timeline based control or in the over/down states of a button. What's so exciting about the use of sound in Flash 5, then? Well, now if we need dynamic control of sound during runtime, we can achieve this through ActionScript. In other words, we can now dynamically adjust the volume and pan of a sound- not to mention being able to start and stop sound in sync with programmatic events. I will assume that everyone reading this tutorial is familiar with the basic timeline use of sound, as well as importing sound and compression formats. An understanding of clipEvents, variable usage and scoping will also be assumed. This tutorial will be focusing on the programmatic use of sound. We will begin with a general overview of the sound object and then look at some of its specific applications. Object of Desire The sound object is one of the predefined or "built-in" objects in Flash. Just like the other predefined objects in Flash it comes with a set of methods or predefined abilities. The sound object's methods enable it to start & stop itself, set its volume and pan, and retrieve information on its current volume and pan levels. It can even control how much volume from a specific channel in a stereo sound file will be sent to the left or right speaker! However, the sound object has limitations. There are no predefined methods for the sound object that identify the current position of a sound during playback or even determine the total length of a sound, not to mention quite a few others. We can, however, extend the functionality of the sound object - by creating our own methods. Speaking of extending capabilities, let's first look at the fundamentals of the sound object. Getting Attached Before we do any ActionScripting we need to set the symbol linkage properties in our library. Since sound in this tutorial will not be used in the traditional timeline sense (although we can also use the sound object's methods to control timeline based sounds set to event, start, and stream sync types), we have to let Flash know that this sound should be exported when we publish our movie. Otherwise, the sound file would not be included in our compiled movie. Go to the options fly-out menu near the upper right hand corner of the library, in this menu you should find an item called "linkage." It is here that we have to select "export this symbol" and supply a unique identifier string for our sound file. This identifier should be a unique name not used elsewhere in your movie. In my sample file ("soundObj_01.fla") I have simply assigned it the identifier "loop." We will use this identifier to programmatically attach the sound from our library. The sound object, like some of the other predefined objects, requires a more formal treatment- it must use a constructor function to instantiate the object. We do this by using new Sound(). However, this by itself does very little. We must also refer to the sound object through a variable.
globalSound1 = new Sound();
It is important to note that the constructor function can also receive an optional target parameter that allows the sound object to control sound within a specific movie clip or path. This is essential for controlling multiple sounds independently in your movie. For the purposes of this tutorial, however, we will be creating a sound object without a parameter- this allows us to control all the sounds in the player (hence the variable name globalSound1). Next, we have to attach the sound from the library using the linkage identifier "loop". This adds the sound to our movie at runtime and places it in the loving control of our global sound object.
globalSound1 = new Sound();
Notice that the attachSound method is actually applied to the variable that refers to the sound object. If you look at the first sample file, you will see that this was done in a frame script (walking before flying, remember...). Starting from Zero We can now apply all the methods of the sound object to the variable globalSound1. Two of the most basic methods we can use with the sound object are start() and stop(). These will be applied to two buttons on the parent timeline. The play button has the following code attached to it:
on (press) {
Since we do not want to restart the sound every time somebody clicks on the play button (unless you enjoy overlapping audio chaos!), we will use a conditional statement to test if it is already playing. The stop button, of course, sets the "playing" variable's Boolean value to false.
The start method, as you may have noticed, can accept two optional parameters: secondOffset and loops. The secondOffset value can be an integer or floating point number that tells the start method at what point to begin playback (measured in seconds, e.g. 2 or 2.35). In other words, if we have a ten second sound file in our library and we set the value of secondOffset to five, we begin playback halfway through our sound. Be careful that your offset value does not exceed the total duration of your sound file. If it does you will not hear anything! If you omit this parameter, the default value is zero. The second parameter, loops, accepts a positive integer value that simply tells the sound object how many times it should repeat the playback of a sound. If you would like to play around with these parameter values, initialize different values for the secondOffset and loops variables in the frame script:
globalSound1 = new Sound();
Stop... I've got a Headache Great... We can start our sound when we click on the play button. Now let's stop it (depending on your mood, this is sometimes more important!). Attached to the stop button is the following code:
on (press) {
The stop method, as invoked here, simply stops the playback of our global sound object. The stop method, however, can also accept an optional parameter that is important when dealing with multiple sounds. This parameter, in the form of the sounds unique identifier, can be used for stopping a specific sound regardless of how many sounds exist at that document level. Dealing with multiple sound objects is a tutorial unto itself and I will leave it as such- for a later date. Fine, but NOT so Loud! The sound object uses the setVolume() method to adjust the volume of the sound(s) controlled by the object. Its sibling method is the getVolume() method which determines the volume of the sound(s) under the control of the object. It is usually wise to use the two in conjunction with each other to determine the current volume level before setting a new value. However, in the second source file, ("soundObj_02.fla"), we will just be altering the volume through an input field that is being read by the setVolume() method in an enterFrame ClipEvent. The setVolume() method generally accepts a parameter value between 0 and 100. It can actually go beyond this value range with both positive and negative numbers, however, anything above positive 100 or below negative 100 usually causes distortion in the sound. In other words, a value of negative 100 and positive 100 would achieve the same result. Try inputting different values into the volume input field to test this method.
Go left, No... Go Right! Even though a mono sound contains a single channel outputted to both the left and right speakers, we can still control the balance of the sound between the left and right speakers. The sound object uses the setPan() method to control this technique. The sibling method of setPan() is getPan(). The getPan() method is often used with setPan() for the same reasons outlined with the sibling volume methods. The setPan() method accepts a number usually between negative 100 and positive 100. Negative 100 controls the sound so that it will play entirely in the left speaker, zero distributes the balance evenly between both the left and right speakers, and positive 100 plays the sound entirely in the right speaker. Anything between these values outputs a proportional mix of balance between the left and right speakers. Situations can occur in which this value range is exceeded, however. If the pan value exceeds 100 then the actual value is outputted as 200 minus the current pan value. If the value goes below negative 100, the actual value outputted is negative 200 minus the current pan value. In other words, by exceeding these ranges you are not giving more dominance to one speaker but actually sending additional sound to the other! Sliding Home The third sample file ("soundObj_03.fla") demonstrates a simple means of using a slider's local coordinate system (100 pixels in length with a local coordinate range between negative 50 and 50) and some simple mathematical formulas to output value ranges appropriate to the setPan() and setVolume methods. You can check out the comments added to the code of this file for more details.
After playing with sliders you may want to experiment with other means of generating parameter values for the setPan() and setVolume() methods- such as using a draggable movie clip's _x property to represent pan values and its _y property to represent volume values. For instance, if you have a movie's stage set to the dimensions of 400 x 400 pixels, you could try attaching the following code to a movie clip symbol:
onClipEvent (load) {
onClipEvent (mouseDown) {
onClipEvent (mouseUp) {
onClipEvent (enterFrame) {
I hope that this helps with your basic
understanding of the sound object and that you are able to use this as a
stepping stone to greater audio heights!
|
||||
|
|
©2001 Ultrashock.com Inc. - All rights reserved
|
|