| Register
Sunday, May 11, 2008   

Controlling Sound Part 2

Created By  Benjamin Mace, at  3/12/2006 - 5 comments.

Click to view this author's website.

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.









Need Professional Help For Your ActionScript Project?
ActionScript.com Consulting Services provide top quality professional ActionScript consulting to businesses around the globe. If you have a professional project in need to world-class talent, tell us about your project by requesting a quote today.

Reader Comments

  1. vanessa miller  Replied:
    ( 4/4/2006 At 12:45 AM)

    It is possible to make a "fade in, fade out" control button?

    I want to make an across scenes sound, every scene with a single stop start button with fade in out effect.

    Is that possible??

  2. Benjamin Mace  Replied:
    ( 4/6/2006 At 4:05 AM)

    Sure it is. All you need to do is setup a setInterval , onEnterFrame or other loop and increase / decrease the volume with each pass of the loop. volume + 1 or volume - 1 for example.

  3. Eric Wenger  Replied:
    ( 5/8/2006 At 3:50 PM)

    Benjamin,

    The question that I have is this. I use sound objects all the time, but they export on the 1st frame of my timeline, and thus they load before my preloader can load and run. How do I pre-load my sound objects, or at least display the loading of them? Do they need to be exported in the first frame?

  4. amany abu  Replied:
    ( 6/20/2006 At 3:10 AM)

    i use the following code
    var _mySound:Sound = new Sound();
    _mySound.loadSound("url that return me the song", false);
    _mySound.onLoad = function(success:Boolean) {
    if (success == true) {
    _mySound.start(0,2);
    }
    }
    the problem is when i press the play button it load song and then play sound as soon as i am on my computer or on my network
    when testing out of my network it you will not hear sound ,it need you to press stop button and then play to hear sound
    this example areadly used in the following url
    http://www.nemra1.com/Public/MusicPage.aspx
    please tray to see it. if it work right tell me else tell me what is the solution of this issue

  5. Boyan Balkanski  Replied:
    ( 9/28/2006 At 12:41 PM)

    Is it possible to add other stuff like frequencies control or just something different from the built-in properties and methods? I really need some extra controls for an order. Please, help :)

Login to post your comments. If you do not have an account with us please Register.
Copyright 2005 by ActionScript, Inc.   |  Privacy Statement  |  Terms Of Use  |  ActionScript Client Extranet