PDA

View Full Version : Control sound volume


dc2000
11-06-2008, 01:53 AM
Hi everyone:


I have the following code to play a lengthy mp3 file (that is imported in the library as is linked as CTestSound class):
var st:SoundTransform = new SoundTransform();

var snd:CTestSound = new CTestSound();
snd.play(0, 0, st);Then I have a volume slider on the stage that sends SliderEvent.CHANGE event that should control the volume of the playback:
function onSliderChange(evt:SliderEvent):void
{
st.volume = Number(evt.value - evt.target.minimum) / Number(evt.target.maximum - evt.target.minimum);
}But nothing happens with the sound.

Can someone tell me how to control the volume of a sound that is already playing?

Mazoonist
11-06-2008, 03:15 AM
var st:SoundTransform = new SoundTransform();
var snd:CTestSound = new CTestSound();
var sc:SoundChannel = snd.play(0, 0, st);
In the line in which you tell the sound to play, you need to save the result of that in a SoundChannel variable. Having a sound channel instance allows you to control the sound in many ways.

Meanwhile, there is an instance of SoundTransform that's a property of the SoundChannel class. However, you can't manipulate this property directly. In other words, you can't do this:
sc.soundTransform.volume -= 0.1; //can't do this!
Instead, you have to create a SoundTransform object of your own (which you've done), set its volume property, then reassign it back to the sound channel. This reassignment has to be performed every time you want to change the volume.
function onSliderChange(evt:SliderEvent):void
{
st.volume = Number(evt.value - evt.target.minimum) / Number(evt.target.maximum - evt.target.minimum);
sc.soundTransform = st;
}

dc2000
11-06-2008, 07:01 AM
Thanks, it worked!