0

how to play audio through the phone speaker using MediaPlayer while the user is on a phone call through BT headphones.

I tried this:

...
AudioDeviceInfo audioDeviceInfo = getPhoneSpeaker(context);
if (audioDeviceInfo != null) {
    setPreferredDevice(audioDeviceInfo);
}
...

@RequiresApi(Build.VERSION_CODES.M)
public AudioDeviceInfo getPhoneSpeaker(Context context) {

    AudioDeviceInfo audioDeviceInfo = null;

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
        AudioManager manager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        if (manager != null) {
            AudioDeviceInfo[] audioDeviceInfos;
            audioDeviceInfos = manager.getDevices(AudioManager.GET_DEVICES_OUTPUTS);
            if (audioDeviceInfos != null) {
                for (AudioDeviceInfo adi : audioDeviceInfos) {
                    if (adi.getType() == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER) {
                        audioDeviceInfo = adi;
                    }
                }
            }
        }
    }

    return audioDeviceInfo;

}

This causes audio to play through the phone speaker, but the problem is that when I play audio the call is terminated in headphones and played through the phone speaker.

Any ideas about how I can avoid this conflict?

Ali Arasteh
  • 79
  • 2
  • 7
  • Note: You can play back the audio data only to the standard output device. Currently, that is the mobile device speaker or a Bluetooth headset. You cannot play sound files in the conversation audio during a call. https://developer.android.com/guide/topics/media/mediaplayer.html – Gobu CSG Jun 30 '22 at 13:32

2 Answers2

1

Audio Can only be played to standard output devices, speakers etc. It is not possible to play sound during a call.

Badar Khalil
  • 126
  • 9
-1

Try this Twilio Audio Switch SDK

Link: Audio Switch Library

SDK: implementation 'com.twilio:audioswitch:$version-SNAPSHOT'

Listener for the devices

audioSwitch.start { audioDevices, selectedDevice ->
// TODO update UI with audio devices
}

Finding available devices

val devices: List<AudioDevice> = audioSwitch.availableAudioDevices 
val selectedDevice: AudioDevice? = audioSwitch.selectedAudioDevice
Gobu CSG
  • 653
  • 5
  • 7