Not able to read the data from the MIC
I am trying to read the data from the MIC and process it and store it in a file. But i am not getting any data from the MIC, the buffer is all zeroes.
int MIN_BUF = AudioRecord.getMinBufferSize(8000,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT);
AudioRecord recorder = new AudioRecord(
MediaRecorder.AudioSource.MIC, 8000,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT, MIN_BUF);
byte[] pcm_in = new byte[320];
recorder.startRecording();
while(record)
{
int bytes_read = recorder.read(pcm_in, 0, pcm_in.length);
switch(bytes_read)
{
case AudioRecord.ERROR_INVALID_OPERATION:
case AudioRecord.ERROR_BAD_VALUE:
Log.i("Microphone", "Error in reading the data");
break;
default:
print(pcm_in);
break;
}
}
recorder.stop();
recorder.release();
But in the print(pcm), when i printed byte by byte i am getting all zeroes. Some posts are there in stackoverflow with similar issues, but my issue didn't got fixed with that.
Please help me in fixing this.
Thanks &a开发者_如何学Cmp; Regards,
SSuman185print(pcm_in) will show you the actual data. you need to get the pcm_in data to pcm in a loop till you stop recording.
Meaning that your variable record is a boolean right. and you will make it false in another method. so till you make it false recorder.read(pcm_in, 0, pcm_in.length) operation will get the data from your mic and put it into the pcm_in(so you need to be sure that the size of pcm_in is equal to pcm). the bytes_read will be the size of the bytes read in this operation. so you can copy the pcm_in bytes to pcm in a loop that can read whole pcm_in data.
for example:
bytes_read = recorder.read(pcm_in, 0, pcm_in.length);
for(int i=0; i<bytes_read ;i++){
pcm[i] = pcm_in[i];
}
But this is a weird usage. I think your pcm should be as large as the file you need to load in it. and make sure you are addin the pcm_in to it , not overriding. I think this is what you want.
精彩评论