tags:

views:

154

answers:

1

Hello, I have made an application that records from the phones microphone using the AudioRecord and 16-bit encoding, and I am able to playback the recording. For some compatibility reason I need to use 8-bit encoding, but when I try to run the same program using that encoding I keep getting an Invalid Audio Format. my code is :

int bufferSize = AudioRecord.getMinBufferSize(11025, 
AudioFormat.CHANNEL_CONFIGURATION_MONO, 
AudioFormat.ENCODING_PCM_8BIT);
AudioRecord recordInstance = new AudioRecord(
MediaRecorder.AudioSource.MIC, 11025,
 AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_8BIT,
bufferSize);

Any one knows what is the problem? according to the documentation AudioRecord is capable of 8-bit encoding. thanks in advanced maxsap.

A: 

If you look at the source, it only supports little endian, but Android is writing out big endian. So you have to convert to little endian and then 8-bit. This worked for me and you can probably combine the two:

for (int i = 0; (offset + i + 1) < bytes.length; i += 2) {
    lens[i] = bytes[offset + i + 1];
    lens[i + 1] = bytes[offset + i];
}
for (int i = 1, j = 0; i < length; i += 2, j++) {
    lens[j] = lens[i];
}

Here is a simpler version without endian

for (int i = 0, j = 0; (offset + i) < bytes.length; i += 2, j++) {
    lens[j] = bytes[offset + i];
}
man910