無限迴圈聲音

import flash.net.URLRequest;
import flash.media.Sound;
import flash.events.Event;

var req:URLRequest = new URLRequest("filename.mp3"); 
var snd:Sound = new Sound(req);

snd.addEventListener(Event.COMPLETE, function(e: Event)
{
    snd.play(0, int.MAX_VALUE); // There is no way to put "infinite"
}

在呼叫 play() 函式之前,你也無需等待聲音載入。所以這將做同樣的工作:

snd = new Sound(new URLRequest("filename.mp3"));
snd.play(0, int.MAX_VALUE);

如果你真的想出於某種原因無限時間迴圈聲音(int.MAX_VALUE 會迴圈播放聲音大約 68 年,不計算 mp3 導致的暫停……)你可以這樣寫:

var st:SoundChannel = snd.play();
st.addEventListener(Event.SOUND_COMPLETE, repeat);
function repeat(e:Event) { 
    st.removeEventListener(Event.SOUND_COMPLETE, repeat);
    (st = snd.play()).addEventListener(Event.SOUND_COMPLETE, repeat);
}

play() 函式每次呼叫時都會返回 SoundChannel 物件的新例項。我們將它變為變數並監聽其 SOUND_COMPLETE 事件。在事件回撥中,將從當前 SoundChannel 物件中刪除偵聽器,併為新的 SoundChannel 物件建立新偵聽器。