动作脚本开/关声音不正常

时间:2014-07-26 21:45:00

标签: actionscript-3 flash soundchannel

基本上我有两个按钮开/关。如果我在声音播放时多次单击按钮ON,则OFF按钮不再起作用,因此我无法停止声音。有人可以帮忙吗?

import flash.media.Sound;
import flash.media.SoundChannel;
import flash.events.MouseEvent;
var mySound:Sound = new Classical_snd();
var myChannel:SoundChannel = new SoundChannel();
myChannel.stop();

soundON_btn.addEventListener(MouseEvent.CLICK, soundON);

function soundON(event:MouseEvent):void{
    myChannel = mySound.play();
}

soundOFF_btn.addEventListener(MouseEvent.CLICK,soundOFF);

function soundOFF(event:MouseEvent):void{
   myChannel.stop();
}

1 个答案:

答案 0 :(得分:1)

之所以发生这种情况,是因为每次调用mySound.play()新的SoundChannel对象进行回放时,该函数调用都会生成并返回声音。因此,如果您将其调用两次,则最新的SoundChannel对象将存储在您的myChannel变量中;但是,生成的任何早期SoundChannel对象都会丢失,因为您不再引用它并继续播放。

我会试试这个:

import flash.media.Sound;
import flash.media.SoundChannel;
import flash.events.MouseEvent;
var mySound:Sound = new Classical_snd();
var myChannel:SoundChannel = new SoundChannel();
myChannel.stop();
var musicPlaying:Boolean = false;

soundON_btn.addEventListener(MouseEvent.CLICK, soundON);

function soundON(event:MouseEvent):void{
    if( !musicPlaying ) {
        myChannel = mySound.play();
        musicPlaying = true;
    }
}

soundOFF_btn.addEventListener(MouseEvent.CLICK,soundOFF);

function soundOFF(event:MouseEvent):void{
   myChannel.stop();
   musicPlaying = false;
}