控制Flash CS5中按钮上的声音

时间:2011-07-05 21:44:00

标签: flash actionscript-3 button audio

我是闪光灯的新手,请原谅这个超级初学者的问题。

在我的flash文档中,我在页面上创建了几个按钮,当用户按下给定按钮时会触发声音。附加的音频长度为1-2分钟,因此我想让用户可以选择关闭给定乐曲的声音。我已经通过页面上的按钮使用以下AS3来完成:

import flash.media.SoundMixer; 
myButton.addEventListener(MouseEvent.CLICK,fn_clickHandler); 
function fn_clickHandler(IN_Event:MouseEvent):void
{         
SoundMixer.stopAll();     
}

我想确保用户一次不会播放多个声音,有人可以告诉我如何应用它吗?

谢谢

1 个答案:

答案 0 :(得分:1)

我不太确定我完全理解你的问题。但如果我做得对,那么这就是答案:

import flash.events.MouseEvent;
import flash.media.Sound;
import flash.media.SoundChannel;

var SChannel : SoundChannel;
var sound : Sound;

// your sound managing buttons
sound_1.addEventListener ( MouseEvent.CLICK, handleChangeSound );
sound_2.addEventListener ( MouseEvent.CLICK, handleChangeSound );
sound_3.addEventListener ( MouseEvent.CLICK, handleChangeSound );

// sound off button
turn_off.addEventListener ( MouseEvent.CLICK, turnOffSound );

// sound off managining function
function turnOffSound ( e : MouseEvent ) : void
{
    SChannel.stop();
    SChannel = null;
    sound = null;
}

// are sounds in your library Sound_1 (), Sound_2 (), Sound_3 ()
function handleChangeSound ( e : MouseEvent ) : void
{
    // in my situation since the button names were sound_1, .._2, .._3
    // i can extract the sound id from names of the button.
    // if you have some different approach you just need to adopt it.
    var soundID : uint = e.target.name.split ( '_' )[1];

    // ifi sound is playing, stop it
    if ( SChannel )
    {
        turnOffSound(null);
    }

    // create new sound, from ID given
    switch ( soundID )
    {
        case 1 :
            sound = new Sound_1 ();
            break;

        case 2 : 
            sound = new Sound_2 ();
            break;

        case 3 :
            sound = new Sound_3 ();
            break;
    }

    // if sound was in the list start playing it.
    if ( sound )
    {
        SChannel = sound.play();
    }
}

顺便说一下,只有当你引用整个flash页面才能保持沉默时才使用SoundMixer