我正在从蓝牙耳机的麦克风录制实时音频并将其播放到手机的扬声器上。
流式代码:
int bufferSize = AudioRecord.getMinBufferSize(samplingRate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
arec = new AudioRecord(MediaRecorder.AudioSource.VOICE_COMMUNICATION, samplingRate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize);
//STREAM_MUSIC - no sound
//STREAM_ALARM - beep from phone's speaker
//STREAM_RING - no sound
//STREAM_NOTIFICATION - no sound
//STREAM_SYSTEM - no sound
//STREAM_VOICE_CALL - plays back on bt headset
//STREAM_DTMF - sound from loud speaker, not very clear but works for now
atrack = new AudioTrack(AudioManager.STREAM_VOICE_CALL, samplingRate, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize, AudioTrack.MODE_STREAM);
atrack.setPlaybackRate(samplingRate);
byte[] buffer = new byte[bufferSize];
arec.startRecording();
atrack.play();
while(isRecording) {
arec.read(buffer, 0, bufferSize);
atrack.write(buffer, 0, buffer.length);
}
蓝牙耳机mic代码配置:
audioManager.setBluetoothScoOn(true);
audioManager.startBluetoothSco();
//works for MODE_IN_COMMUNICATION - however there's a lot of echo when on speaker phone
//works fine for MODE_IN_CALL
audioManager.setMode(AudioManager.MODE_IN_CALL);
//turning speakerphone on causes a lot of disturbance
//audioManager.setSpeakerphoneOn(true);
//samplingRate = AudioTrack.getNativeOutputSampleRate(AudioManager.STREAM_VOICE_CALL);
samplingRate = 16000;
但是,即使我有setSpeakerphoneOn(false)
耳机并将耳机插入音频插孔,音频也是从手机本身而不是耳机播放的。我该如何克服呢?打开扬声器会产生很大的噪音。
我使用的是 16000 的采样率,因为它可以在没有耳机的情况下听到。在我使用原生采样率的那一刻,我显然只是听到了真正的高频噪音。我怎样才能让这个声音在耳机而不是手机的扬声器上播放?