如何使用键盘输入更改声音的音量? [英] How to change the volume of a Sound using keyboard input?
问题描述
code> private var sound:Sound;
private var channel:SoundChannel;
private const INCREMENT:Number = 0.2; //将其改为你喜欢的
sound = new Sound();
sound.addEventListener(Event.COMPLETE,onLoad);
sound.load(new URLRequest(song.mp3));
函数onLoad(e:Event):void
{
channel = sound.play();
if(stage)
{
stage.addEventListener(KeyboardEvent.KEY_UP,onKey);
else
trace(从舞台上的显示对象调用);
函数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)//音量范围从0到1
vol = 0;
if(vol> 1)
vol = 1;
tr.volume = vol;
channel.soundTransform = tr;
}
How can I increase/decrease the volume of a Sound on key-press in ActionScript 3.0
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;
}
这篇关于如何使用键盘输入更改声音的音量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!