[英]looping sound flash as3
我已經使用Flash AS3創建了聲音的開/關按鈕。 這些有效,但是每當我按關閉按鈕然后按打開按鈕時,音樂就不再播放了嗎?
我認為這是一個循環的問題,但是我能說錯嗎? 我不確定如果出現循環問題要使用什么代碼。
我還需要為btnOn函數添加代碼,因為當我打開.swf時,聲音會自動播放。
附件是我當前的代碼如下:
var mySound:Sound = new sandstorm(); //(sandstorm is my sound file)
var myChannel:SoundChannel = new SoundChannel();
var lastPosition:Number = 0;
myChannel = mySound.play();
btnOff.addEventListener(MouseEvent.CLICK, onClickPause);
function onClickPause(e:MouseEvent):void {
lastPosition = myChannel.position;
myChannel.stop();
}
干杯:)
您的代碼僅顯示事件偵聽器onClickPause(我想這就是您的停止按鈕)。 但是開始/播放按鈕的事件監聽器在哪里。 在播放按鈕上,您必須再次調用播放功能。 這是一個很棒的教程: http : //www.republicofcode.com/tutorials/flash/as3sound/
您可以嘗試下面的代碼。 它使用一個按鈕實現音頻暫停/恢復功能...
var mySound:Sound = new sandstorm(); //(sandstorm is my sound file)
var myChannel:SoundChannel = new SoundChannel();
var lastPosition:Number = 0;
var audioState : String = "paused"; //will become either "playing" or "paused"
myChannel = mySound.play(); //this line starts playback
audioState = "playing"; //update because you started playback with above line
btnOff.addEventListener(MouseEvent.CLICK, onPlayPause);
function onPlayPause(e:MouseEvent):void
{
if (audioState == "playing") //IF already playing
{
lastPosition = myChannel.position; //note current "audio time" when pausing
myChannel.stop(); //stop playback
audioState = "paused"; //update for next time click is used
}
else if (audioState == "paused") //or ELSE IF was already paused then...
{
myChannel = mySound.play(lastPosition); //resume playback
audioState = "playing"; //update for next time click is used
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.