Controlling the Volume


So you have your sound attached and started. Now what? It's time to start using some of the more exciting methods of the Sound object. As already mentioned, you can use ActionScript to control the volume and panning of a sound attached to a Sound object. Now, take a look at how you can control the volume of a sound.

The setVolume method of the Sound object enables you to set the volume of a sound attached to a Sound object. Imagine thatsetVolume sets the volume! The syntax for the setVolume method is as follows :

 mySound.setVolume(volume); 

The volume argument is a number from 0100, and it represents the volume level. The higher the number, the louder the volume. If you set the volume to 0, the sound continues to play, but you won't be able to hear it.

Note

Although you should not be able to go above 100 or below 0 with the volume argument, Flash lets you do it. Be careful with this, however, because setting the volume to a very high number (1000, for example) or a very low number (-1000, for example) might blow your speakers ! After the number in the volume argument goes below 0, the volume starts to go up instead of down.


After the volume of the sound has been set, the getVolume method is used to retrieve the value of the volume setting. The getVolume method has this syntax:

 mySound.getVolume(); 

The getVolume method returns the last value set for the volume. If the sound has just started, and no setVolume methods have been applied, the volume will be at 100. The returned value should be an integer between 0 and 100.

Note

Because you actually can set the volume above or below the specified range using the setVolume method, it stands to reason that the getVolume method can return a value outside that range. It can, and will, return whatever value you set using the setVolume method, even if that number is below 0 or above 100. It returns the value as an integer.


Working with a Single Sound

Your sound has started. Now you can use ActionScript to control its volume with the setVolume method. You're going to make a slider to control the volume of the sound.

Exercise 18.4 Controlling the Volume of a Sound

You're going to apply some ActionScript to the Volume Control movie clip so that its current X position affects the volume of the mySound object.

  1. Make sure you still have volume.fla open . If you're still in Symbol-Editing mode for the Volume Controller movie clip, choose Edit > Edit Movie to switch to the main movie.

  2. Select the instance of the Volume Controller movie clip in frame 35 of the Volume Controller layer on the main timeline.

  3. Open the Actions panel, and add the following ActionScript to the instance of the Volume Controller:

     onClipEvent (enterFrame) { } 

    The onClipEvent handler lets you apply ActionScript to movie clips. The event that you're using in this case is enterFrame, which triggers the ActionScript contained by the handler after a frame in the movie clip plays, even if there's only one frame. The ActionScript in the onClipEvent handler with the enterFrame event occurs after the ActionScript on the frame in the movie clip has played .

  4. Inside the onClipEvent handler, add the following ActionScript:

     newVolume = 100-((rightSide-this._x)/rightSide)*100; 

    Your ActionScript now should look like this:

     onClipEvent (enterFrame) {     newVolume = 100-((rightSide-this._x)/rightSide)*100;  } 

    Remember the rightSide variable that was initialized in frame 1 of the Volume Controller movie clip? That variable sets the right boundary of the startDrag method that controls the dragging of the slider. You are using the rightSide variable to calculate the value of a variable newVolume, based on the current X position of the Volume Controller movie clip.

Note

Some real numbers turn the equation from Step 4 into something a little easier to swallow. Assume that the current X position is 100.

  • Subtract the current X position of the movie clip from the rightSide variable. 550-100 = 450.

  • Divide the value by the value assigned to rightSide. 450550 = 0.82 (approximately).

  • Multiply the value by 100. 0.82x100 = 82.

  • Finally, subtract the value from 100. 100-82 = 18.

The value of newVolume is 18 when the current X position is 100.

This means that as you move the slider to the left (the lower X position), the volume is decreased. As you move your slider to the right (the higher X position), the volume is increased.


  1. After the line containing the newVolume variable, add the following ActionScript:

     mySound.setVolume(newVolume); 

    This line takes the value for newVolume and uses the setVolume method to apply it to the mySound object. This sets the volume of the sound attached to the object to the value of the variable. (See Figure 18.6.) Using the example from the Note, the sound would now have a volume of 18, which is pretty low.

    Figure 18.6. The ActionScript applied to the instance of the Volume Controller movie clip.

    graphics/18fig06.gif

  2. Choose Control > Test Movie.

    When the movie plays, you should hear the same sound that you heard in the last exercise. Notice that its volume is set considerably lower nowthat's because the mySound object is now being affected by a setVolume method. Because the Volume Controller movie clip starts in the middle of the screen (X = 275), the value of the newVolume variable is 50: 100-((550-275)550) x 100 = 50

  3. Move the Volume Controller movie clip to the left and right.

    As you move the slider to the right, the volume should increase. If you move it to the left, the volume should decrease. (See Figure 18.7.) That's because the value of newVolume increases as the X position increases and decreases as the X position decreases.

    Figure 18.7. Move the movie clip to the left.

    graphics/18fig07.gif

You're done with this movie, so you can close it and save it before you move on.

Working with Multiple Sounds

The setVolume method actually works on an entire timeline at once, not just on a single sound. Thus, if you have multiple attached sounds in a single movie clip, or on the main timeline, applying the setVolume method to any Sound object in that timeline affects all the Sound objects in that timeline. Give it a try.

Exercise 18.5 Controlling the Volume of Multiple Sound Objects in a Single Movie Clip

The setVolume method actually controls all the Sound objects in a single timeline. You can use this to your advantage, as this exercise demonstrates .

  1. Open volume_mix.fla from the Chapter_18/Assets folder on the CD. This movie has three layers (see Figure 18.8), each of which has a total of 35 frames .

    Figure 18.8. The movie contains three layers.

    graphics/18fig08.gif

    The three layers are as follows:

    • Actions. This layer has a keyframe in frame 35. That keyframe contains a Stop action.

    • Volume Controller. This layer has an instance of the Volume Controller movie clip in frame 35. This movie clip is nearly identical to the one you worked with in volume.fla.

    • Background. This layer contains the Background Clip movie clip, which is identical to the one in volume.fla.

  2. Take a look at the sounds in the Sounds folder of the Library. (See Figure 18.9.) There are six sounds, each of which has an identifier. The identifiers are sound1, sound2, sound3, sound4, sound5, and sound6.

    Figure 18.9. There are six sounds in the Sounds folder, each of which is exported with the movie.

    graphics/18fig09.gif

  3. Select frame 35 of the Volume Controller layer and double-click the instance of the Volume Controller movie clip that's on the Stage to open it in Symbol-Editing mode.

  4. Select the instance of the Button symbol in frame 1 of the Button layer and take a look at the ActionScript that's attached to it:

     on (press) {     this.startDrag(false, leftSide, topSide, rightSide, bottomSide);  }  on (release) {     this.stopDrag();  } 

    This ActionScript is identical to the ActionScript that you looked at on the button in Step 4 of Exercise 18.1.

  5. Take a look at the ActionScript applied to frame 1 of the Actions layer of the movie clip:

     leftSide = 0;  rightSide = 550;  topSide = this._y;  bottomSide = this._y;  maxSounds = 6; 

    This ActionScript is nearly identical to the ActionScript you saw in the last exercise, with the exception of the last line. The last line gives a variable named maxSounds a value of 6.

  6. Add the following after the ActionScript in frame 1 of the Actions layer:

     for (i=1; i<=maxSounds; i++) {     this["sound" + i] = new Sound(this);      this["sound" + i].attachSound("sound" + i);      this["sound" + i].start(0, 9999);  } 

    This ActionScript makes use of a for loop to create several Sound objects, attach the appropriate sound to each object, and start each sound. Notice that the condition for the for loop is i<=maxSounds, so the loop continues until the value of i is less than or equal to maxSounds. This causes the loop to run six times, making six Sound objects and attaching and starting each new object.

    Note

    When you start several sounds at once, they might not always synchronize properly. This can be especially obvious for longer sounds; in this exercise, you probably won't notice it because the sounds are all very short. If you run into this problem, try starting each sound and then stopping the sounds. Then start all the sounds againthey should synchronize a little better. If you want to make the start-stop-start less obvious, you can set the volume of each sound to 0 when you start it the first time and then set it to the appropriate level when you start it the second time. You learn how to set the volume later in this chapter.

  7. Select Edit > Edit Movie from the main menu to return to the main timeline. Select the instance of the Volume Controller movie clip on the Stage in frame 35 of the Volume Controller movie clip and add the following ActionScript to this movie clip:

     onClipEvent (enterFrame) {     newVolume = 100-((rightSide-this._x)/rightSide)*100;      this["sound" + maxSounds].setVolume(newVolume);  } 

    Some of ActionScript should look familiar; you used it in Exercise 18.4, Steps 4 and 5, to control the volume of a single Sound object in the Volume Controller movie clip. In this case, you're still applying a setVolume method to a Sound object, but this time, you're using a variable to name the Sound object. The following ActionScript:

     this["sound" + maxSounds].setVolume(newVolume); 

    is evaluated as

     this.sound6.setVolume(50); 

    when the newVolume variable is equal to 50. Remember that you have a variable called maxSounds, with a value of 6, in the Volume Controller movie clip.

    It doesn't matter which Sound object you target in this ActionScript because all the sounds that you want to control are in a single timeline.

  8. Choose Control > Test Movie and test your volume controller. When you move it to the left or right, the volume should decrease or increase.

  9. Close the Test Movie window and save the movie.

Now that you know how to control the volume, it's time to take a look at controlling the pan, or left-right balance, of a sound.



Inside Flash
Inside Flash MX (2nd Edition) (Inside (New Riders))
ISBN: 0735712549
EAN: 2147483647
Year: 2005
Pages: 257
Authors: Jody Keating

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net