2011-11-16 103 views
0

到目前为止,我的代码停止声音,但一旦停止声音不再起作用。我没有看到任何错误报告。Flash AS3音频播放器似乎只是停止工作

这是因为如果该事件侦听器不再侦听...

stop(); 
var soundClip:Sound = new Sound(); 
var sndChannel:SoundChannel = new SoundChannel(); 
var soundClipCompleted = false; 
var isPlaying = false; 
soundClip.load(new URLRequest("tune.mp3")); 
soundClip.addEventListener(Event.COMPLETE,onComplete,false,0,true); 
function onComplete(evt:Event):void { 
    sndChannel=soundClip.play(); 
    soundClipCompleted=true; 
    isPlaying = true; 
    Action_BtnPlay.gotoAndStop(2); 
} 
Action_BtnPlay.addEventListener(MouseEvent.MOUSE_UP, playBtn); 
function playBtn (evt:MouseEvent):void { 
    if(isPlaying && soundClipCompleted){ 
     sndChannel.stop(); 
     Action_BtnPlay.gotoAndStop(1); 
    }else{ 
     sndChannel = soundClip.play(); 
     Action_BtnPlay.gotoAndStop(2); 
    } 
} 

回答

0
stop(); 
var soundClip:Sound = new Sound(); 
var sndChannel:SoundChannel = new SoundChannel(); 
var soundClipCompleted = false; 
var isPlaying = false; 
soundClip.load(new URLRequest("tune.mp3")); 
soundClip.addEventListener(Event.COMPLETE,onComplete,false,0,true); 
function onComplete(evt:Event):void { 
sndChannel=soundClip.play(); 
soundClipCompleted=true; 
isPlaying = true; 
Action_BtnPlay.gotoAndStop(2); 
} 
Action_BtnPlay.addEventListener(MouseEvent.MOUSE_UP, playBtn); 
function playBtn (evt:MouseEvent):void { 
if(isPlaying && soundClipCompleted){ 
    sndChannel.stop(); 
    Action_BtnPlay.gotoAndStop(1); 
    isPlaying=false; 
    soundClipCompleted=false; 
}else{ 
    sndChannel = soundClip.play(); 
    Action_BtnPlay.gotoAndStop(2); 
    } 
} 

您应该设置isPlaying和soundClipCompleted变量为false。

+0

非常感谢:) –

+0

@ WebOD-Bradley:如果你的问题已经解决,那么你可以接受答案。 –

0

为两台isPlayingsoundClipCompletedtrue当声第一次调度complete事件,所以在那之后,if语句您playBtn处理程序将始终评估为true并停止声音。

+0

非常感谢:) –