我正在使用 AudioRecord 捕获音频数据包并将它们流式传输到语音识别服务器。在我的 Galaxy Note 4、Android M 设备中,它运行得非常好。但是,当我使用其他设备(Nexus 7/Android L 和 HTC combo/android ICS)时,生成的音频是断断续续的,声音中每半秒就会出现一次故障噪音,这会破坏服务器的语音识别过程。
我知道这是一个复杂的话题,有人知道如何处理 android 中的这种音频捕获异常吗?
这是我的代码设置:
private static final int AUDIO_SOURCE = MediaRecorder.AudioSource.VOICE_RECOGNITION;
private static final int SAMPLING_RATE = 16000; //44100,
private static final int CHANNEL_CONFIG = AudioFormat.CHANNEL_IN_MONO;
private static final int BIT_RATE = AudioFormat.ENCODING_PCM_16BIT;
bufferSize = AudioRecord.getMinBufferSize(SAMPLING_RATE,
CHANNEL_CONFIG,
BIT_RATE;
audioBuffer = new byte[bufferSize];
AudioRecord recorder = new AudioRecord(AUDIO_SOURCE,
SAMPLING_RATE,
CHANNEL_CONFIG,
BIT_RATE,
bufferSize);
recorder.startRecording();
while (isRecording) {
short[] buffer = new short[bufferSize];
int shorts_recorded = recorder.read(buffer, 0, buffer.length);
byte[] audioBytes = new byte[bufferSize*2]; //bufferSize*2
ByteBuffer.wrap(audioBytes).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().put(buffer);//runningBuffer.get(0));
Integer.toString(audioBytes.length)+","+audioBuffer.length);
handler.onAudioDataCapture(audioBytes); //expose audio data to upload callback interface
proceed();
}