Recipe 13.11 Setting the Volume of a Sound

13.11.1 Problem

You want to set the volume of a sound.

13.11.2 Solution

Use the setVolume( ) method of the Sound object that controls the sound.

13.11.3 Discussion

You can use Sound.setVolume( ) to set the volume on a scale of 0 to 100, in which 0 is mute and 100 is the loudest:


It is possible to set the volume beyond the range of 0 to 100. If you set the volume to less than 0, Flash converts the volume setting to its absolute value. For example, if you set the volume to -50, Flash plays the sound at 50% volume (+50). If you exceed the value of 100, the sound gets increasingly distorted. The exception to this is if you are setting the volume of a parent Sound object in which the nested Sound objects' volumes are less than 100. For example, if your nested Sound object has a volume of 50 and you set the parent Sound object to a volume of 200, the result is that the nested sound seems to play at full (100 percent) volume.

// Create two Sound objects. One targets the current timeline, and the other targets
// a nested timeline.
parent_sound = new Sound(this);
nested_sound = new Sound(this.soundHolder_mc);

// Attach a sound to the nested Sound object.

// Play the nested sound.
nested_sound.start(  );

// Set the volume of the nested sound to 50 and the volume of the parent sound to
// 200. The result is that the nested sound seems to play at full volume.

Sound.setVolume( ) sets the volume to an absolute value. Regardless of the previous volume setting, the preceding example sets the volume to an absolute value of 50. You can use getVolume( ) in conjunction with setVolume( ) to set the volume relative to the current value.

The getVolume( ) method returns the sound's current volume level. You can add or subtract a number to or from the current value to increment or decrement the sound's volume relatively:

// Increment the current volume by 6.
mySound_sound.setVolume(mySound_sound.getVolume(  ) + 6);

The volume value is given in percentage of perceived loudness. This means that 100 sounds twice as loud as 50.

It is also important to note that ActionScript controls only the volume of sounds within the Flash Player. It cannot control the user's system volume or speaker volume. This means that if the user has his system volume or speaker volume turned all the way down, he will not hear any sounds. There is also no way to detect the user's current system or speaker volume settings.

Because each Sound object targets a movie clip, there exists a parent/child relationship between Sound objects that corresponds to the parent/child relationship of the target movie clips. This does not mean that one Sound object can target another using _parent, but the volume (or panning) settings applied to a "parent" Sound object affect the resultant volumes (or panning) of "child" sounds. For example, if you set the volume of a Sound object targeting _root to 50, all nested sounds play at half their own volume settings. Therefore, you can easily adjust the volume of all sounds in your movie by creating a Sound object that targets _root and setting that object's volume. For example:

master_sound = new Sound(_root);

// Mute all sounds.

13.11.4 See Also

Recipe 13.16

    Part I: Local Recipes
    Part II: Remote Recipes