我有一个 android 应用程序,它使用 AudioRecord 对象记录音频并通过通知获取数据。我每 64 毫秒请求一次通知,并得到它,但有时,在某些手机上,当我从 AudioRecorder 对象进行实际读取时,需要几秒钟才能返回。知道什么可能导致这样的事情吗?
下面是我所做的工作的骨架。
private void GetRecorder()
{
int sampleRate = 16000;
int framePeriod = (int)(sampleRate * 64/ 1000);
int bufferSize = framePeriod * 16/ 8;
recorder = new AudioRecord(AudioSource.MIC, sampleRate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize);
recorder.setPositionNotificationPeriod(framePeriod);
recorder.setRecordPositionUpdateListener(this.mRecordListener);
}
private OnRecordPositionUpdateListener mRecordListener = new OnRecordPositionUpdateListener()
{
long start = System.currentTimeMillis();
long readTime = 0;
long maxReadTime = 0;
int frames = 0;
@Override
public void onPeriodicNotification(AudioRecord local_recorder)
{
int buffPos = 0;
int framePeriod = (int)(16000 * 64 / 1000);
long beforeRead = System.currentTimeMillis();
int readBytes = local_recorder.read(bufferQue.pool[queLocation].audioBuffer, buffPos, Math.min(framePeriod, framePeriod - buffPos));
long afterRead = System.currentTimeMillis();
if (afterRead - beforeRead > maxReadTime) maxReadTime = afterRead - beforeRead;
readTime += afterRead - beforeRead;
//This section each 10 secs if didn't get enough frame and print the data
frames++;
if (System.currentTimeMillis() - start > 10000)
{
if (frames < 110)
{
log4j.info("In last 10 secs: " + frames + ", read time was " + readTime + " Max read time was " + maxReadTime);
}
readTime = 0;
maxReadTime = 0;
frames = 0;
start = System.currentTimeMillis();
}
}
@Override
public void onMarkerReached(AudioRecord recorder) {
}
};