2011-11-16 105 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

非常感謝:) –