How can I increase/decrease the volume of a Sound on key-press in ActionScript 3.0
A:
You will need to use listeners to listen to KeyboardEvents e.g.
http://www.bensilvis.com/?p=146
And to change the volume you will need to use a SoundTransform on the SoundChannel returned when you play the sound e.g.
vitch
2009-12-04 13:22:09
+3
A:
private var sound:Sound;
private var channel:SoundChannel;
private const INCREMENT:Number = 0.2;//change it as you like
sound = new Sound();
sound.addEventListener(Event.COMPLETE, onLoad);
sound.load(new URLRequest("song.mp3"));
function onLoad(e:Event):void
{
channel = sound.play();
if(stage)
{
stage.addEventListener(KeyboardEvent.KEY_UP, onKey);
}
else
trace("call this from a display object on stage");
}
function onKey(e:KeyboardEvent):void
{
var tr:SoundTransform = channel.soundTransform;
var vol:Number = tr.volume;
if(e.keyCode == Keyboard.UP)
vol += INCREMENT;
else if(e.keyCode == Keyboard.DOWN)
vol -= INCREMENT;
if(vol < 0)//volume ranges from 0 to 1
vol = 0;
if(vol > 1)
vol = 1;
tr.volume = vol;
channel.soundTransform = tr;
}
Amarghosh
2009-12-04 13:33:37
+1
A:
package {
import flash.display.Sprite;
import flash.events.KeyboardEvent;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundTransform;
import flash.net.URLRequest;
import flash.ui.Keyboard;
public class Main extends Sprite
{
public function Main()
{
sound = new Sound(new URLRequest("http://assets.flashstall.com/mp3/Estelle - American Boy (sample).mp3"));
soundChannel = sound.play();
stage.addEventListener(KeyboardEvent.KEY_UP, stage_onKeyUp);
}
private const INCREMENT_STEP:Number = .1;
private var sound:Sound;
private var soundChannel:SoundChannel;
private var soundTransform_:SoundTransform = new SoundTransform();
private function stage_onKeyUp(e:KeyboardEvent):void
{
if(!soundChannel) return;
switch(e.keyCode)
{
case Keyboard.UP:
if(soundChannel.soundTransform.volume >= 1) break;
soundTransform_.volume = soundChannel.soundTransform.volume + INCREMENT_STEP;
soundChannel.soundTransform = soundTransform_;
break;
case Keyboard.DOWN:
if(soundChannel.soundTransform.volume <= 0) break;
soundTransform_.volume = soundChannel.soundTransform.volume - INCREMENT_STEP;
soundChannel.soundTransform = soundTransform_;
break;
}
}
}
}
This should do it.
Emanuil
2009-12-04 15:29:13
This is correct but it seems unnecessarily complicated.
Goose Bumper
2009-12-05 06:05:56
A:
When you call the play() method on a sound object it returns the SoundChannel it is playing on.
var channel:SoundChannel = soundObject.play();
You can then set the volume of that sound using the soundTransform property of the SoundChannel.
channel.soundTransform = new SoundTransform(0.5); // Sets the volume to 50%
More often you want to control the volume of ALL sounds playing in your movie. This can be done by setting the soundTransform property of the SoundMixer class.
SoundMixer.soundTransform = new SoundTransform(0.5); // Sets the global volume to 50%
Chris
2009-12-04 17:38:58