0

我需要一些关于我的 actionscript 3 项目的帮助。我有一个按钮,里面有声音。我有一些代码(见下文),当我按下按钮时它会播放声音,如果我再次按下按钮它会停止声音(如静音/取消静音按钮)。问题是,当我第二次按下按钮播放声音时,它会播放两个声音(两次相同的声音),如果我按下按钮多次播放声音,相同的声音会播放很多次。你能帮我解决问题吗?谢谢你。

   function setMute1(vol){
       sTransform1.volume = vol; 
       SoundMixer.soundTransform = sTransform1;
  }

   var sTransform1:SoundTransform = new SoundTransform(1,0);
   var Mute1:Boolean = true;
   sound1_btn.addEventListener(MouseEvent.CLICK,toggleMuteBtn1);

   function toggleMuteBtn1(event:Event) {
    if(Mute1 === false) {
        Mute1 = true;
        setMute1(0);
     } else {
         Mute1 = false;
         setMute1(1);
     }
  }
4

2 回答 2

4

据我了解,您通过将声音分配给按钮命中帧来启动声音?您需要通过代码启动声音才能以良好的方式控制声音。

这是一个基于您的代码的工作示例,它加载一个外部mp3文件。通过同一个按钮播放和停止声音。

// load the sound
var mySound:Sound = new Sound();
mySound.load(new URLRequest("loop.mp3"));
var myChannel:SoundChannel = new SoundChannel();
// tool you need for manipulating the volume;
var sTransform1:SoundTransform = new SoundTransform(1,0);
// The sound starts not muted
var Mute1:Boolean = true;
var vol:Number = 0;

sound1_btn.addEventListener(MouseEvent.CLICK,toggleMuteBtn1);
// Set the sound volume;
function setMute1(vol)
{
    sTransform1.volume = vol;
    SoundMixer.soundTransform = sTransform1;
    // Check if sound is muted
    if (vol<=0)
    {
        Mute1 = true;
    }
    else
    {
        Mute1 = false;
    }
}
// Start/stop sound
function startOrStop()
{
    if (Mute1 === false)
    {
        myChannel.stop();
        setMute1(0);
    }
    else
    {
        setMute1(1);
        myChannel = mySound.play();
    }
}
// This happens when you click the buttom
function toggleMuteBtn1(event:Event)
{
    startOrStop()
}

actionscript 2中有一个可以停止所有声音的功能,在actionscript 3中你不能再这样做了,但你仍然可以将声音分配给帧。

于 2013-08-04T15:06:45.660 回答
1

此示例将声音静音和取消静音。声音没有停止,只是静音。此外,这里的声音必须在代码中分配,而不是分配给框架。

// load the sound
var mySound:Sound = new Sound();
mySound.load(new URLRequest("loop.mp3"));
var myChannel:SoundChannel = new SoundChannel();
// tool you need for manipulating the volume;
var sTransform1:SoundTransform = new SoundTransform(1,0);
// The sound starts not muted
var Mute1:Boolean = true;
var vol:Number = 0;

sound1_btn.addEventListener(MouseEvent.CLICK,toggleMuteBtn1);
// Set the sound volume;
function setMute1(vol)
{
    sTransform1.volume = vol;
    SoundMixer.soundTransform = sTransform1;
    // Check if sound is muted
    if (vol<=0)
    {
        Mute1 = true;
    }
    else
    {
        Mute1 = false;
    }
}
// Toggle mute on/off
function toggleMute()
{
    if (Mute1 === false)
    {
        setMute1(0);
    }
    else
    {
        setMute1(1);
    }
}
// This happens when you click the buttom
function toggleMuteBtn1(event:Event)
{
    // if not playing, the sound
    if (myChannel.position != 0) {
    } else {
        myChannel = mySound.play();
    }

    toggleMute();
}
于 2013-08-04T09:21:24.950 回答