【发布时间】:2023-04-09 06:37:01
【问题描述】:
我有一个简单而经典的声明,即每 200 毫秒播放一个声音(节拍器)。
我使用处理程序编写它,然后以另一种方式使用线程。 两种方式的问题都是一样的:当我按下硬件主页按钮时,或者当我按下按钮打开 ListView 时,节拍器会严重减慢一段时间。
这个问题(不是很严重,但存在)也表示什么都不做,让应用程序处于前台。
有什么想法吗?
代码如下:
公共类 Metronome 实现 Runnable{
private Handler mHandler = new Handler();
public static long mStartTime;
Main mainContext;
public Metronomo(Main context) {
mainContext = context;
}
public void play() {
mStartTime = System.currentTimeMillis();
mHandler.postDelayed(this, 100);
}
public final void stop(){
mHandler.removeCallbacks(this);
}
public void run(){
//play the ogg file in position 1
mSoundManager.playSound(1);
//reschedule the next playing after 200ms
mHandler.postAtTime(this, SystemClock.uptimeMillis() + 200);
}
};
【问题讨论】:
-
这是此类设备上精确计时的问题。完全不确定是否有解决方案,更不用说一个简单的解决方案了
标签: android timer real-time slowdown