This second tutorial on sound is going to get you up and
running with the sound''s volume and pan methods. As with the first article, you
should have a basic grasp of ActionScript and by now understand how to create
and attach a sound object to the stage.
Let''s start off with the volume method. When dealing with
volume we can both set and get the volume of the current sound object. Both
methods are very easy to use. Assuming we created the following sound object
(from Part 1) we''d have this script so far:
1) myLoop = new
Sound();
2) myLoop.attachSound("loopSample");
3) myLoop.start(0,10);
To set the volume all we need to do is add the following
afterward:
4) myLoop.setVolume(50);
Here 50 is our volume level in a value range from 0-100
where 0 is the lowest/off level. Ideally in an application you would want to
have a setVolume method of you own for the sound you are using that would
accept the new volume as a parameter. For example you might want to have a
function in a given class that accepts the following:
1) function SetMusicVolume (newVolume:Number):Void
2) {
3) myLoop.setVolume(newVolume);
4) }
Getting the current volume is a bit simpler. It works like
so:
5) myLoop.getVolume();
Of course this won''t do anything for you. This method
returns a string so you''ll need to use it somehow even if to just trace it to
your output window.
5) trace(myLoop.getVolume());
In a given class you way want to have a method to return the
volume of a sound.
1) function GetMusicVolume ():Number
2) {
3) return
myLoop.getVolume();
4) }
This method would return a number from 0-100 for myLoop.
The pan or “balance” methods work in a similar fashion but
their range is a bit different. Where volume works from 0-100, pan works from
-100-100 where 0 is “balanced” or equal volume levels on both left and right
channels and -100 is left only. Set pan in our first example would work as
follows:
6) myLoop.setPan(-100); // left speaker only
6) myLoop.setPan(0); // centered sound
6) myLoop.setPan(100); // right speaker only
Getting our current pan works in the same manner as our
getVolume method where it simply returns a number representing the current pan.
As with our volume you would ideally setup a get a set method for the pan in a
given class.
1) function SetMusicPan (newPan:Number):Void
2) {
3) myLoop.setPan(newPan);
4) }
1) function GetMusicPan ():Number
2) {
3) return
myLoop.getPan();
4) }
These custom methods for a new class in your application would ideally
be set with a slider component or a knob type component. These controllers
would pass in the parameters listed in the sample methods as numbers for the
new levels. Keep in mind that volume and pn don't need to be manually set by the user. The use of sound settings can come in handy with game applications where sound effects come from the sides of the screen or volume is set by distance from the user's screen. Experimenting with these sample functions and different ways to set them will help you understand their use better.