我正尝试将动画同步到特定BPM中的音乐。我试过使用计时器,但在处理毫秒级的小间隔时不准确。我做了一些阅读,并找到了一种替代方法,它使用一个小型无声音频文件和SOUND_COMPLETE事件作为Timer。AS3中准确的BPM事件监听器
我用这个代码使用了167ms长的声音文件。
package
{
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLRequest;
public class BetterTimer extends EventDispatcher
{
private var silentSound:Sound;
public function BetterTimer(silentSoundUrl:String):void {
super();
silentSound = new Sound(new URLRequest(silentSoundUrl));
silentSound.addEventListener(Event.COMPLETE, start);
}
public function start():void {
this.timerFired(new Event("start"));
}
private function timerFired(e:Event):void {
dispatchEvent(new Event("fire"));
var channel:SoundChannel = silentSound.play();
channel.addEventListener(Event.SOUND_COMPLETE, timerFired, false, 0, true);
}
}
}
这仍然没有停留在节拍上。 Flash Player的声音是否准确?
我刚刚给了BeatTimer一试。这是写在一个特定的BPM季度节拍踢事件。我测试了它的八分之一和十六分之一,它在我的2分钟循环期间保持准确。我也想尝试Joa的解决方案,但为了我的目的,这个BeatTimer运作良好。谢谢。 – knuckfubuck 2009-12-08 07:17:55