Android:我想从麦克风读取缓冲区,以便我可以对其执行处理,以下是我的代码
int sampleRateInHz = 8000;// 44100, 22050 and 11025
int channelConfig = AudioFormat.CHANNEL_CONFIGURATION_MONO;
int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
//int bufferSize =11025 +
int bufferSize = AudioRecord.getMinBufferSize(sampleRateInHz,channelConfig, audioFormat);
short[] buffer = new short[bufferSize];
AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleRateInHz,channelConfig, audioFormat, bufferSize);
if(audioRecord.getState()== AudioRecord.STATE_INITIALIZED){
audioRecord.startRecording();
Log.e("recording", "before");
boolean flag = true;
while (flag) {
int bufferReadResult = audioRecord.read(buffer, 0, bufferSize);
System.out.println(buffer);
}
audioRecord.stop();
audioRecord.release();
}
Log.e("recording", "stopeed");
<uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>
每次我尝试测试程序时都会出现以下错误
06-04 00:18:17.222: E/AudioRecord-Java(488): [android.media.AudioRecord] 初始化本机 AudioRecord 对象时出现错误代码 -20。