我正在尝试使用AudioRecord
,但我无法正确初始化录制内容。我有两个设备,其中一个设备运行良好,但另一个继续提供例外。
我的代码:
bufferSize = AudioRecord.getMinBufferSize(8000,AudioFormat.CHANNEL_IN_STEREO, AudioFormat.ENCODING_PCM_16BIT);
AudioRecord recorder = new AudioRecord(AudioSource.DEFAULT, rate,channelConfig, audioFormat, bufferSize);
为什么这是错误的,这样做的正确方法是什么?
答案 0 :(得分:2)
private int[] mSampleRates = new int[] { 8000, 11025, 22050, 44100 };
int bufferSize;
AudioRecord audioInput = findAudioRecord();
public AudioRecord findAudioRecord() {
for (int rate : mSampleRates) {
for (short audioFormat : new short[] {
AudioFormat.ENCODING_PCM_8BIT,
AudioFormat.ENCODING_PCM_16BIT }) {
for (short channelConfig : new short[] {
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.CHANNEL_IN_STEREO }) {
try {
Log.d("Mic2", "Attempting rate " + rate
+ "Hz, bits: " + audioFormat
+ ", channel: " + channelConfig);
bufferSize = AudioRecord.getMinBufferSize(rate,
channelConfig, audioFormat);
if (RECORDINGDURATION * sampleRate != AudioRecord.ERROR_BAD_VALUE) {
// check if we can instantiate and have a
// success
AudioRecord recorder = new AudioRecord(
AudioSource.DEFAULT, rate,
channelConfig, audioFormat, bufferSize);
if (recorder.getState() == AudioRecord.STATE_INITIALIZED)
sampleRate = rate;
return recorder;
}
} catch (Exception e) {
Log.e(TAG, rate + "Exception, keep trying.", e);
}
}
}
}
return null;
}
该设备很可能不支持16位编码 - > AudioFormat.ENCODING_PCM_16BIT: - )
答案 1 :(得分:2)
模拟器不支持CHANNEL_CONFIGURATION_STEREO& sampleRate =(11025,16000,22050和44100),如果你想成功运行,那么使用CHANNEL_CONFIGURATION_MONO& SampleRate = 8000(每秒仅支持8000个样本)。
以下是对该怎么做的详细说明:http://developer.android.com/guide/topics/media/audio-capture.html