actionscript-3 - FLASH AS3声音重叠

标签 actionscript-3 flash audio

好吧,我完全是Flash as3的傻瓜,所以我想这必须很容易解决。我正在制作一个用Flash CS6录制声音的音板,非常简单:1帧,十个按钮,每个按钮发出不同的声音。问题是这些声音重叠,所以我需要的是当我按下一个按钮时其他声音停止播放。有人吗?

最佳答案

请参阅 play() 类中的 Sound 方法文档,它返回一个 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中创建另一个放屁按钮应用程序。那就是你要做的:
  • 创建一个按钮符号,将其添加到阶段并在“属性”选项卡中为其指定实例名称。我们称之为myButton
  • 使用file-> import
  • 将声音添加到库中
  • 将此声音导出到 ActionScript 。右键单击库中的声音,在“ ActionScript 选项卡”上选中“导出为 ActionScript ”,“在第1帧导出”。用所需的声音(例如MySound)的类名称填充“类”输入
  • 然后,您必须在单击按钮时触发声音播放。因此,应将以下代码放入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);
    
  • 关于actionscript-3 - FLASH AS3声音重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32986576/

    相关文章:

    arrays - 什么时候在 as3 中使用 ArrayCollection 或 Array?

    apache-flex - Flex textarea 控件未正确更新

    actionscript-3 - 为什么我不能从其他类访问公共(public)变量?

    flash - 是否有与 Adob​​e P2P Flash 视频等效的 HTML 5?

    html - 音频标签在 Firefox 和 Opera 中不起作用

    actionscript-3 - 从动态文本字段调用 AS3 函数

    javascript - .@ 是 Javascript 符号吗?

    javascript - 如何检测 flashblocker?

    iOS - 连接混音器、remoteio 和动态处理器的 AudioFormat 问题

    audio - 如何创建多个jwplayer播放按钮