问题描述
我正在尝试以特定 BPM 将动画与音乐同步.我试过使用 Timer,但在处理以毫秒为单位的小间隔时它不准确.我做了一些阅读并找到了一种替代方法,它使用一个小的无声音频文件和 SOUND_COMPLETE 事件作为计时器.
I'm trying to sync animation to music at a specific BPM. I've tried using the Timer but it isn't accurate when dealing with small intervals in milliseconds. I did some reading and found an alternate method that uses a small silent audio file and the SOUND_COMPLETE event as a Timer.
我在这段代码中使用了 167 毫秒长的声音文件.
I used 167ms long sound file with this code.
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 的声音是否准确?
This still doesn't stay on beat. Is the Flash Player capable of accuracy with sound?
推荐答案
这很难做到!有一个名为 BeatTimer 的小库试图做到这一点.我自己还没有尝试过这段代码,但如果它按照它声称的那样做,它应该正是您所需要的.
This is very tricky to get right! There's a small lib called BeatTimer that tries to do this. I haven't tried this code myself, but if it does what it claims it should be exactly what you need.
这篇关于AS3 中准确的 BPM 事件监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!