我正在Android设备上录制音频,需要获取正在进行的录制的精确时间戳。在记录文件的后处理期间,这些时间戳将与一些其他数据相关。目前我通过创建新的计时器线程获得以下时间戳:
private Handler customHandler = new Handler();
private Runnable updateTimerThread = new Runnable() {
public void run() {
timeInMilliseconds = SystemClock.uptimeMillis() - startHTime;
customHandler.postDelayed(this, 0);
}
};
我开始录音后立即重置计时器:
mRecorder.start();
startHTime = SystemClock.uptimeMillis();
customHandler.postDelayed(updateTimerThread, 0);
完成录制后,我会销毁handler
,它会获取时间戳并进行一些计算并终止计时器:
mRecorder.stop();
handler.removeCallbacksAndMessages(null);
customHandler.removeCallbacks(updateTimerThread);
这是handler
的定义方式:
handler = new Handler();
handlerDelay = 100; //milliseconds
handler.postDelayed(new Runnable() {
public void run() {
MeasuredPoint point = getMeasuredPoint(); // this part records the timestamp and does some intense computations to calcluate 3D points.
handler.postDelayed(this, handlerDelay);
}
}, handlerDelay);
但是,我的上一个时间戳的值总是比录音的持续时间高约300ms,但这不应该发生,因为我在停止录音之前杀了定时器线程。有谁知道为什么会这样?