javaandroidaudio-streamingandroid-bluetoothheadset

Realtime audio playing on headphones while recording audio from bluetooth


I am recording audio from Bluetooth headsets. I want to play this recorded audio in real time on my wired headphones. How can I direct my audio to wired headphones.

I am using below code for directing audio.

mAudioManager.setSpeakerphoneOn(false);
mAudioManager.setWiredHeadsetOn(true);
mAudioManager.setRouting(AudioManager.MODE_IN_COMMUNICATION, AudioManager.ROUTE_HEADSET,AudioManager.ROUTE_BLUETOOTH_SCO );

Above code works on android version 5 but does not work on android version 7 and above.

Need Help in:

  1. why it is not working in new android versions
  2. How can I make it work on newer versions.

Solution

  • I solved it using below code. It works perfect for old versions but audio directed to wired headphones in newer version is noisy. I will try to solve the noise problem and will update the answer.

    AudioDeviceInfo[] devices = mAudioManager.getDevices(AudioManager.GET_DEVICES_OUTPUTS);
        if (devices  != null){
          for (AudioDeviceInfo device : devices)
             if (device.getType() == AudioDeviceInfo.TYPE_WIRED_HEADSET || device.getType() == AudioDeviceInfo.TYPE_WIRED_HEADPHONES) {
                audioTrack.setPreferredDevice(device);
                mAudioManager.setWiredHeadsetOn(true);
                mAudioManager.setSpeakerphoneOn(false);
              }
         }