I use AudioRecord
to record audio on Android device. To create a instance of AudioRecord
, you should set a fixed size of buffer
to store the audio data. Then, we need pull data from this buffer
continuously. If the size of buffer
is too small and fetching data is too slow, the buffer
will be overflow !
To avoid this exception, I want to set the size of buffer
larger as far as possible and fetching data in time.
AudioRecord
provides the getMinBufferSize(int,int,int)
method to get a min size of buffer
that audio hardware can support. But this is mininum size not the proper size of buffer
.
My question is how to calculate and set the proper size of the buffer
?
Here is what I using the AudioRecord
:
audioRecord = new AudioRecord(audio_source, sampleRate, audio_channel, audio_encoding, buffer_size);
new Thread(new Runnable{
while(true){ //Loop start
readSize = audioRecorder.read(readBuffer, 0, useReadBufferSize); // fetch data from buffer to readbuffer
// throw readbuffer data out, do this operation as qucik as possible, Avoid to block thread
// check if the AudioRecord stopped and break if true
}
}).start();