I have built voice recording feature in my app and have launched it in production for few months. From error logs, I have found that sometimes, the read() method in AudioRecord will return different number of bytes than indicated in sizeInBytes argument.
public int read(byte[] audioData, int offsetInBytes, int sizeInBytes)
Below is the code snippet to help understand better.
val audioRecordData = ByteArray(bufferSize)
val length = audioRecord.read(audioRecordData, 0, audioRecordData.size)
if (length == AudioRecord.ERROR_BAD_VALUE || length == AudioRecord.ERROR_INVALID_OPERATION || length != audioRecordData.size) {
// Quitting because Audio record encountered fatal exception
throw RuntimeException()
}
Question: Is it safe to continue calling read() even though the number of bytes returned by audioRecord.read() is not equal to the sizeInBytes?
Yes. It is safe to continue.
The javadoc for that method says:
In your example, I can think of two scenarios where the result will be greater than zero and less than
audioRecordData.size.You are close to the end of an audio file, and there aren't enough unread bytes left to fill the buffer you provided.
You are reading an audio stream from a device or a remote source in "real time" ... and the
readis delivering all of the audio bytes available right now.Basically, your application needs to act appropriately when
readreturns fewer bytes than you asked for.For what it is worth, the
ERROR*values are all less than zero, so the correct interpretation of the result is:result < 0- an error as indicated by the error coderesult == 0- end of streamresult > 0- a successful read ... and the result value is the number of bytes (or shorts) read into the supplied array orByteBuffer.