启动后AudioRecord返回一些空数据 [英] AudioRecord returns some empty data after start

查看:488
本文介绍了启动后AudioRecord返回一些空数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个标准代码,用于使用AudioRecord从麦克风接收数据。这是我的代码:

I wrote a standard code for receiving data from microphone by using AudioRecord. Here is my code:

AudioReceiver() {
    int minHardwareBufferSize = AudioRecord.getMinBufferSize(SAMPLE_RATE,
            CHANNEL_CONFIG, AUDIO_FORMAT);
    Log.d(TAG, "minHardwareBufferSize = " + minHardwareBufferSize);
    int bufferSizeBytes = (minHardwareBufferSize > MIN_BUFFER_SIZE_BYTES) ?
            minHardwareBufferSize : MIN_BUFFER_SIZE_BYTES;
    bufferSizeShorts = bufferSizeBytes / 2;

    //резервируем буфер с запасом в 2 раза
    audioRecorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
            SAMPLE_RATE, CHANNEL_CONFIG, AUDIO_FORMAT, bufferSizeBytes * 2);

    testStack = new short[bufferSizeShorts * 4];
    Arrays.fill(testStack, (short) 2000);
}


boolean startReceive() {
    audioRecorder.startRecording();
    isReceiving = true;
    int recordingState = audioRecorder.getRecordingState();
    Log.d(TAG, "recordingState = " + recordingState);
    new Thread(receivingRunnable).start();
    return (recordingState == AudioRecord.RECORDSTATE_RECORDING);
}


boolean stopReceive() {
    isReceiving = false;
    audioRecorder.stop();
    int recordingState = audioRecorder.getRecordingState();
    Log.d(TAG, "recordingState = " + recordingState);
    return (recordingState == AudioRecord.RECORDSTATE_STOPPED);
}


private Runnable receivingRunnable = new Runnable() {
    @Override
    public void run() {
        int readCount = 0;
        short[] dataBuffer = new short[bufferSizeShorts];
        while (isReceiving) {
            testBusy = true;
            for (int j = 0; j < 4; j++) {
                readCount = audioRecorder.read(dataBuffer, 0, dataBuffer.length);
                Log.d(TAG, "receive " + readCount + " bytes");
                System.arraycopy(dataBuffer, 0, testStack, bufferSizeShorts * j, readCount);
            }
            isReceiving = false;
            testBusy = false;
        }
    }
};

但是我注意到在startReceive函数第一次启动之后,在testStack缓冲区的开头,有空数据(Nexus 4上大约有1000个示例,请参见数据图)。

But I noticed that after the first start of the startReceive function, at the beginning of the testStack buffer, there are empty data (about 1000 samples on the Nexus 4, see data graph).

在AudioReceiver的初始化和startReceive的启动之间需要很长时间。

Between the initialization of AudioReceiver and the launch of startReceive takes a long time. What could be the cause of the problem?

推荐答案

似乎是由于OS花时间重新采样数据而引起的延迟,很难为所有设备提供一个采样率。在其他帖子中也提到过。因此,我改为将音频记录逻辑移到活动 onCreate 中。当我不需要音频或音频被暂停时,我只是丢掉了数据。

It seems delay is caused when OS takes time to resample the data, and it is difficult to have a one sample rate for all devices. Also mentioned by other posts. So, I instead moved my audio record logic to activity onCreate. When I dont need the audio or it is paused, I just throw away the data.

在始终记录音频时,我没有发现电池消耗过多。虽然这可能不是解决此问题的最佳方法,但这绝对是我可以接受的解决方法。

I did not observe heavy battery drain while always on an audio record. While this might not be the best way to go about fixing this issue, but this definately was an acceptable workaround for me.

这篇关于启动后AudioRecord返回一些空数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆