AudioRecord returns some empty data after start

大兔子大兔子 提交于 2019-12-24 14:42:34

问题


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;
        }
    }
};

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).

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


回答1:


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.



来源:https://stackoverflow.com/questions/45786636/audiorecord-returns-some-empty-data-after-start

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!