scr*_*pse 27

这不会给你完美的无间隙播放,但它会导致声音循环.

var sound:Sound = new Sound();
var soundChannel:SoundChannel;

sound.addEventListener(Event.COMPLETE, onSoundLoadComplete);

sound.load("yourmp3.mp3");


// we wait until the sound finishes loading and then play it, storing the
// soundchannel so that we can hear when it "completes".
function onSoundLoadComplete(e:Event):void{
    sound.removeEventListener(Event.COMPLETE, onSoundLoadComplete);
    soundChannel = sound.play();
    soundChannel.addEventListener(Event.SOUND_COMPLETE, onSoundChannelSoundComplete);
}

//  this is called when the sound channel completes.
function onSoundChannelSoundComplete(e:Event):void{
    e.currentTarget.removeEventListener(Event.SOUND_COMPLETE, onSoundChannelSoundComplete);
    soundChannel = sound.play();
}
Run Code Online (Sandbox Code Playgroud)

如果您想要通过完美无缝的无间隙播放多次循环声音,您可以拨打电话

sound.play(0, 9999); // 9999 means to loop 9999 times
Run Code Online (Sandbox Code Playgroud)

但是如果你想在第9999次播放后想要无限播放,你仍然需要设置一个完整的声音监听器.这种做事方式的问题是你必须暂停/重启声音.这将创建一个soundChannel,其持续时间比实际声音文件的持续时间长9999倍,并且当持续时间长于声音长度导致可怕的崩溃时调用播放(持续时间).

  • sound.play(0,int.MAX_VALUE);,这是2,147,483,647. (5认同)
  • +1.使用loop参数既简单又可以提供更好的结果.如果将它设置为足够大的数字,则不需要关心`SOUND_COMPLETE`.作为一个`int`,我想你可以把它设置为最大值0x7fffffff((2 ^ 31) - 1),这对于所有实际目的来说应该足够了. (2认同)

fra*_*mes 10

var sound:Sound = whateverSoundYouNeedToPlay;
function playSound():void
{
    var channel:SoundChannel = sound.play();
    channel.addEventListener(Event.SOUND_COMPLETE, onComplete);
}

function onComplete(event:Event):void
{
    SoundChannel(event.target).removeEventListener(event.type, onComplete);
    playSound();
}
Run Code Online (Sandbox Code Playgroud)