简体   繁体   English

用于节拍器的android中的高分辨率计时器

[英]high resolution timer in android for metronome

I'm trying to create a metronome for Android, and I wrote this code to play a beep at 300BPM using a SoundPool, but it tends to skip beats sometimes and creates lag. 我正在尝试为Android创建一个节拍器,我编写了这段代码,使用SoundPool以300BPM的速度发出提示音,但是有时它会跳过节拍并产生延迟。 I have researched, and all of the answers I have found don't solve my issue. 我已经进行了研究,发现的所有答案都无法解决我的问题。 This especially happens when I try to speed up the bpm count or use eighth notes instead of quarter (double time). 当我尝试加快bpm计数或使用八分音符而不是四分音符(两次)时,尤其会发生这种情况。 Can someone guide me in the right direction of making this as accurate as possible? 有人可以指导我朝着尽可能正确的方向发展吗? Delay/beat-skipping is not acceptible . 不能接受延迟/跳音。 Thanks!! 谢谢!!

     final SoundPool sndPool = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);
     final int sndHigh = sndPool.load(this, R.raw.high, 1);
     setVolumeControlStream(AudioManager.STREAM_MUSIC);
     Thread th = new Thread(new Runnable(){
          public void run() {
            while(true){
                sndPool.play(sndHigh, 1f, 1f, 1, 0, 1f);
                //Log.d("asd", "beep");
                LockSupport.parkNanos(((240000/300)/4)*1000000); //300bpm
            }
          }
     });
     th.setPriority(Thread.MAX_PRIORITY);
     th.start();

The solution is not to use a timer or SoundPool at all. 解决方案是根本不使用计时器或SoundPool。 Instead use a low-level AudioTrack that you manage continuously. 而是使用您连续管理的低级别AudioTrack。 You will have to synthesize the beep or click or whatever by inserting the appropriate samples into the AudioTrack at the proper point in the buffer. 您必须通过在缓冲区中的适当位置将适当的样本插入到AudioTrack中来合成蜂鸣声或单击或其他任何声音。 Just Google AudioTrack and look for samples on how it is used. 只是Google AudioTrack,并寻找有关其用法的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM