将AudioRecord对象的采样率设置为44100时出现错误。当它是22050时,它可以正常工作。
02-16 10:45:45.099 24021-24021/com.vlad.jackcomms E/AudioRecord:frameCount 1024
02-16 16:45:45.099 24021-24021/com.vlad.jackcomms E/AudioRecord-JNI:创建AudioRecord实例时出错:初始化检查失败。
02-16 16:45:45.099 24021-24021/com.vlad.jackcomms E/android.media.AudioRecord:初始化 native AudioRecord对象时出现错误代码-20。
02-16 10:45:45.109 24021-24021/com.vlad.jackcomms E/AndroidRuntime:致命
异常(exception):主要
流程:com.vlad.jackcomms,PID:24021
java.lang.IllegalStateException:在未初始化的AudioRecord上调用startRecording()。
以下是相关代码:
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();
最佳答案
别忘了也要请求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);
}