How to change the volume of a Sound using keyboard input?

﹥>﹥吖頭↗ 提交于 2019-12-25 04:55:29

问题


How can I increase/decrease the volume of a Sound on key-press in ActionScript 3.0


回答1:


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;
}



回答2:


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.




回答3:


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%



回答4:


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.

http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/media/SoundTransform.html#includeExamplesSummary



来源:https://stackoverflow.com/questions/1846829/how-to-change-the-volume-of-a-sound-using-keyboard-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!