如何使用键盘输入更改声音的音量?

0 flash actionscript-3

如何在ActionScript 3.0中按键增加/减小音量

Ama*_*osh 5

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;
}
Run Code Online (Sandbox Code Playgroud)