【问题标题】:How to Adjust Volume in an Audio loop?如何在音频循环中调整音量?
【发布时间】:2016-10-31 11:43:07
【问题描述】:

有人会如何改变循环播放的音乐的音量?例如,我正在制作一款游戏,并且在某个帧我希望将音乐级别 (music.wav) 降低到其音量的一半。

有人怎么能在 AS3 中做到这一点?

【问题讨论】:

  • @JonnyHenly 但是,帧加载事件怎么样?
  • 您是否查看了我链接的问题的答案中的代码,特别是function onKey(...):void { ... } 代码?我相信它会类似于那个代码。
  • 让我为你google that... PS:通过Sound API 加载您的音频文件,这样使用代码更容易控制(即:不要在时间轴上插入) - PPS:为什么不能是 MP3?您将获得比 WAV 格式更好的压缩效果(未压缩 = 更大的文件)。

标签: actionscript-3 flash audio adobe flash-cs3


【解决方案1】:

您以令人困惑的方式使用“循环”一词。在编程中,循环通常指的是看起来像这样的“for”循环之一:

for (var i:int = 0; i < 10; i++)
{
   //do stuff 10 times
}

我猜这不是你所说的循环,而是你想要一个MovieClip 或主时间线来减少n 帧末尾的Sound 对象的音量。或者你的意思是音乐本身在循环播放?希望你能看到提出写得很好的问题的价值。话虽如此..

请注意,我还没有尝试过,但根据我的参考书(Lott、Schall 和 Peters 的 ActionScript 3.0 Cookbook),您需要使用 SoundTransform 对象来指定您希望声音达到的音量被设置。试试这个:

var _sound:Sound = new Sound(music.wav); // creates a Sound object which has no internal volume control
var channel:SoundChannel = _sound.play(); // creates a SoundChannel which has a soundTransform property
var transform:SoundTransform = new SoundTransform(); // SoundTransform objects have a property called "volume". This is what you need to change volume.

现在在您的循环中(或您正在使用的帧事件中)执行以下操作:

transform.volume *= 0.9; // or whatever factor you want to have it decrease
//transform.volume /= 1.1; // or this if you prefer.
channel.soundTransform = transform; //

因此,只要您希望音量按此增量减少,请运行这段代码。当然,您需要确保您设置的任何变量都可以在引用它们的代码中访问。想到的一种方法是使用函数。

private function soundDiminish(st:SoundTransform, c:SoundChannel, factor:Number = 0.9):void
{
    st.volume *= factor;
    c.soundTransform = st;
}

现在,只要您想减小音量,只需调用 soundDiminish 函数即可。

也许你的框架事件是这样的:

function onLoadFrame(fe:Event):void
{
    soundDiminish(transform, channel); // 3rd parameter optional
}

如果您只想每 20 帧调用一次此函数,则:

function onLoadFrame(fe:Event):void
{
    // this is a counter that will count up each time this frame event happens
    frameCount ++;
    if (frameCount >= 20)
    {
        soundDiminish(transform, channel); // 3rd parameter optional
        frameCount = 0; // reset the frame counter
    }
}

【讨论】:

  • 非常感谢!这么多信息!这么有见识!我向你致敬,好先生!
  • 不客气。您很快就会回到这里为其他人回答同样的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-18
  • 1970-01-01
相关资源
最近更新 更多