如何使用键盘输入更改声音的音量? [英] How to change the volume of a Sound using keyboard input?

查看:372
本文介绍了如何使用键盘输入更改声音的音量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆