当闪存AS3结束时,如何循环播放声音?

问题描述:

什么AS3代码用于循环使用AS3的声音?当闪存AS3结束时,如何循环播放声音?

+1

你或许应该接受顶部投票答案是正确的。 – Zze 2016-11-02 04:02:07

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(); 
} 

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

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(); 
} 

如果你想用一个完美无瑕的,无缝播放的声音多次循环,你可以叫

sound.play(0, 9999); // 9999 means to loop 9999 times 

但你仍然需要设立度SoundComplete监听器,如果你想无限第9999回放后回放。这种做事方式的问题是,如果你不得不暂停/重新启动声音。这将创建一个声音通道,其持续时间比实际声音文件的持续时间长9999倍,并且当持续时间长于声音长度导致可怕的崩溃时调用播放(持续时间)。

+2

+1。使用循环参数更简单,并为您提供更好的结果。如果您将其设置为足够大的数量,则不需要关心'SOUND_COMPLETE'。作为一个'int',我想你可以将它设置为最大值0x7fffffff((2^31) - 1),这对于所有实际目的应该足够了。 – 2011-03-26 17:00:23

+0

“更好”在这种情况下是主观的。肯定会有更好的声音保真度,但如果因任何原因必须暂停/恢复声音,则需要承受更大的痛苦。 – scriptocalypse 2011-03-26 17:03:00

+0

够公平的。无缝播放意味着我的意思更好。 – 2011-03-26 17:05:03

其他答案很棒,但是如果你不想使用代码(无论出于何种原因),你可以把声音放入一个动画片段中,将sound属性设置为“Stream”,然后添加尽可能多的帧你喜欢电影剪辑以确保它完全播放。

这当然不是首选的方式,但对于动画师来说,我相信它在某些情况下可能更可取(例如,当与动画师想要的环形动画同步时)。

要在@ scriptocalypse的无缝播放扩展位:

没有适当的无缝播放来自MP3,包括关于在任一头部或文件尾部的文件信息的问题(ID3标签等),因此当你尝试循环它时会有小的停顿。根据你的情况,你可以做几件事情。

  1. 忽略它,只是照常播放,在每个文件的末尾稍作停顿。你也可以尝试用另一种声音(拍滴哟)掩盖它,或淡出淡入。
  2. 如果你的声音被嵌入,而不是流,然后创建一个FLA文件,在那里拖动你的MP3,和将它们设置为导出(与为MovieClip添加链接名称的方式相同)。看起来,当您输出这样的声音时,Flash会考虑延迟,或者在创建Sound对象时将其去掉。无论哪种方式,你可以做一个简单的play()传递要为一个无缝播放的循环(我使用loops参数是不是等待的SOUND_COMPLETE事件,并再次发挥它更好的发现)。
  3. 您可以尝试一些ogg库来使用.ogg文件而不是.mp3。一个简单的谷歌搜索“as3 ogg lib”会显示你所需要的。就我个人而言,我发现它们使用起来有些尴尬,而且我无法承受额外的开销(与MP3解码相反,这是在播放器中完成的)。
  4. 如果您的mp3文件正在流式传输,那么获得无间隙播放的唯一方法就是将它们分层。确定差距(取决于你用什么编码它们,它会有所不同 - 我的文件有330毫秒的差距),当你到达它时,开始播放覆盖。如果你正在褪色,这是一个适当的痛苦,但是当它工作时,它会很好地工作。最坏的情况,你最终的情况(1)

import flash.media.Sound; 
import flash.media.SoundChannel; 

var mySound:Sound = new Bgm(); //Bgm() is the class of the internal sound which can be done in the library panel. 

playSound(); 

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

function onComplete(event:Event):void 
{ 
    SoundChannel(event.target).removeEventListener(event.type, onComplete); 
    playSound(); 
} 

这完美的作品。

+0

我完全不知道,你需要导入'flash.events.Event'。另外,更重要的是,在第一次循环之后,我似乎无法让音乐停止;我想停止音乐并通过声音播放游戏。 – audiFanatic 2014-07-05 12:29:12

我想这你在寻找什么的情况下,语音/音乐文件库:

var mysound:my_sound = new my_sound(); 
mysound.play(0,2); // this will repeat the sound 2 times. 

这似乎已经为我工作:

var nowTime:Number = (new Date()).time; 
var timeElapsed:Number = nowTime - _lastTime; 

_lastTime = nowTime; 
_musicTimeElapsed+=timeElapsed; 

if(_musicTimeElapsed >= _musicA.length - GAP_LENGTH) 
{ 
    _musicTimeElapsed = 0; 
    _musicA.play(0); 
}