0

基本上我有两个开/关按钮。如果我在播放声音时多次单击 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();
}
4

1 回答 1

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;
}
于 2014-07-26T22:08:15.550 回答