I have to keep the Audio Speaker even if my headset is plugged into my device using AudioManager, but when wrote this code the AudioManager automatically switched to normal mode after 6 sec. There is another way to do this on android 12?
public void onCreate() {
m_amAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
m_amAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
if (android.os.Build.VERSION.SDK_INT \>= android.os.Build.VERSION_CODES.S) {
setCommunicationDevice(AudioDeviceInfo.TYPE_WIRED_HEADSET);
} else {
m_amAudioManager.setSpeakerphoneOn(true);
}
}
@RequiresApi(api = Build.VERSION_CODES.S)
public void setCommunicationDevice(Integer targetDeviceType) {
List<AudioDeviceInfo> devices = m_amAudioManager.getAvailableCommunicationDevices();
for (AudioDeviceInfo device: devices) {
if (device.getType() == targetDeviceType) {
boolean result = m_amAudioManager.setCommunicationDevice(device);
}
}
}