I'm recording live audio from a bluetooth headset's mic and playing it onto the phone's speaker.
Streaming code:
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);
}
Configuration of bluetooth headset mic code:
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;
However, even when I have setSpeakerphoneOn(false)
and insert a earphones into the audio jack, the audio is played from the phone itself and not the headset. How do I overcome this? Turning the loudspeaker on causes a lot of noise.
I'm using a sampling rate of 16000 because it can be heard without headphones. The moment I use the native sampling rate, I obviously just hear really high frequency noise. How can I get this sound to play on the headset rather than the phone's speaker?