Ali Arasteh
Ali Arasteh

Reputation: 79

playing audio with media player through phone speaker during call

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?

Upvotes: 0

Views: 876

Answers (2)

Badar Khalil
Badar Khalil

Reputation: 134

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

Upvotes: 1

Gobu CSG
Gobu CSG

Reputation: 691

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

Upvotes: -1

Related Questions