java Android:当 SAMPLERATE 设置为 44100 时,在未初始化的 AudioRecord 上调用 startRecording()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28539717/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: startRecording() called on an uninitialized AudioRecord when SAMPLERATE set to 44100
提问by user3333414
I get an error, when I set the sampling rate to 44100 for the AudioRecord object. When it's 22050 it works fine.
当我将 AudioRecord 对象的采样率设置为 44100 时,出现错误。当它是 22050 时它工作正常。
02-16 10:45:45.099 24021-24021/com.vlad.Hymancomms E/AudioRecord﹕ frameCount 1024 < minFrameCount 1792
02-16 10:45:45.099 24021-24021/com.vlad.Hymancomms E/AudioRecord:frameCount 1024 < minFrameCount 1792
02-16 10:45:45.099 24021-24021/com.vlad.Hymancomms E/AudioRecord-JNI﹕ Error creating AudioRecord instance: initialization check failed.
02-16 10:45:45.099 24021-24021/com.vlad.Hymancomms E/AudioRecord-JNI:创建 AudioRecord 实例时出错:初始化检查失败。
02-16 10:45:45.099 24021-24021/com.vlad.Hymancomms E/android.media.AudioRecord﹕ Error code -20 when initializing native AudioRecord object.
02-16 10:45:45.099 24021-24021/com.vlad.Hymancomms E/android.media.AudioRecord:初始化本机 AudioRecord 对象时的错误代码 -20。
02-16 10:45:45.109 24021-24021/com.vlad.Hymancomms E/AndroidRuntime﹕ FATAL
02-16 10:45:45.109 24021-24021/com.vlad.Hymancomms E/AndroidRuntime:致命
EXCEPTION: main Process: com.vlad.Hymancomms, PID: 24021 java.lang.IllegalStateException: startRecording() called on an uninitialized AudioRecord.
例外:主进程:com.vlad.Hymancomms,PID:24021 java.lang.IllegalStateException:在未初始化的 AudioRecord 上调用 startRecording()。
Here's the relevant code:
这是相关的代码:
private static final int RECORDER_SAMPLERATE = 22050*2;
private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_MONO;
private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
RECORDER_SAMPLERATE, RECORDER_CHANNELS,
RECORDER_AUDIO_ENCODING, BufferElements2Rec * BytesPerElement);
recorder.startRecording();
回答by Cédric Dufouil
Don't forget to ask for AUDIO_RECORD permission too
也不要忘记请求 AUDIO_RECORD 权限
private void checkRecordPermission() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECORD_AUDIO},
123);
}
回答by LaurentY
You could check if 44100 is supported by your device. Android does not provide an explicit method to check it but there is a work-around with AudioRecord class' getMinBufferSize function.
您可以检查您的设备是否支持 44100。Android 没有提供一个明确的方法来检查它,但是有一个 AudioRecord 类的 getMinBufferSize 函数的解决方法。
public void getValidSampleRates() {
for (int rate : new int[] {44100, 22050, 11025, 16000, 8000}) { // add the rates you wish to check against
int bufferSize = AudioRecord.getMinBufferSize(rate, AudioFormat.CHANNEL_CONFIGURATION_DEFAULT, AudioFormat.ENCODING_PCM_16BIT);
if (bufferSize > 0) {
// buffer size is valid, Sample rate supported
}
}
}