使用AudioRecord与Android的8位编码 [英] Using AudioRecord with 8-bit encoding in android
问题描述
我已经从使用AudioRecord和16位编码的手机麦克风记录的应用程序,并且我能够回放录像。出于某种原因,兼容性我需要使用8位编码,但是当我尝试使用该编码我不断收到无效的音频格式运行相同的程序。我的code是:
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);
任何人知道这是什么问题?根据该文件AudioRecord能8位编码的。
Any one knows what is the problem? According to the documentation AudioRecord is capable of 8-bit encoding.
推荐答案
如果你看看源代码,它只支持小尾数,但Android是写出大端。所以,你必须转换为小端,然后8位。这为我工作,你也许可以将二者结合起来:
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];
}
这篇关于使用AudioRecord与Android的8位编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!