Using AudioRecord with 8-bit encoding in android

烈酒焚心 提交于 2019-12-25 02:38:52

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/2319907/using-audiorecord-with-8-bit-encoding-in-android

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