【发布时间】:2016-01-04 08:25:45
【问题描述】:
好吧,我是 flash as3 的菜鸟,所以我想这一定很容易解决。我正在制作一个用 flash cs6 录制声音的音板,非常简单:1 帧,十个按钮,每个按钮发出不同的声音。问题是这些声音的重叠,所以我需要的是当我按下一个按钮时,其他声音停止播放。请问有人吗?
【问题讨论】:
标签: actionscript-3 flash audio
好吧,我是 flash as3 的菜鸟,所以我想这一定很容易解决。我正在制作一个用 flash cs6 录制声音的音板,非常简单:1 帧,十个按钮,每个按钮发出不同的声音。问题是这些声音的重叠,所以我需要的是当我按下一个按钮时,其他声音停止播放。请问有人吗?
【问题讨论】:
标签: actionscript-3 flash audio
在播放声音之前将其添加到每个按钮的代码中:
SoundMixer.stopAll();
如果您直接从 Adobe Flash 中的时间线添加动作,则无需导入该类。如果您正在使用像 FlashDevelop 或 FlashBuilder 这样的 IDE,请将此代码添加到开头(Package { 之后):
import flash.media.SoundMixer;
编码愉快!
【讨论】:
查看Sound类中的play()方法文档,它返回一个SoundChannel对象,该对象有一个stop()方法。
所以你可以这样做(示意性地):
var currentChannel:SoundChannel;
button1.addEventListener(MouseEvent.CLICK, onButtonClick);
button2.addEventListener(MouseEvent.CLICK, onButtonClick);
button3.addEventListener(MouseEvent.CLICK, onButtonClick);
function onButtonClick(event:MouseEvent):void
{
/* provided you have implemented selectSoundByButton func somewhere */
const sound:Sound = selectSoundByButton(event.currentTarget);
if (currentChannel) {
currentChannel.stop();
}
currentChannel = sound.play();
}
更详细的说明:
假设您想在 Flash 中创建另一个放屁按钮应用程序。这就是你必须做的:
然后您必须在单击按钮时触发声音播放。所以你应该把下面的代码放在你的 flash 剪辑的第一帧:
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.events.MouseEvent;
var currentChannel:SoundChannel;
const mySound:Sound = new MySound();
function onClick(e:MouseEvent):void {
if (currentChannel) {
currentChannel.stop();
}
currentChannel = mySound.play();
}
myButton.addEventListener(MouseEvent.CLICK, onClick);
【讨论】: