【问题标题】:actionscript on/off sound not working properly动作脚本开/关声音无法正常工作
【发布时间】:2014-09-18 11:50:13
【问题描述】:

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

【问题讨论】:

    标签: actionscript-3 flash soundchannel


    【解决方案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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-08
      • 1970-01-01
      • 1970-01-01
      • 2014-12-07
      • 2015-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多