4

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);
        }
    }
}
paertopeer
  • 41
  • 3
  • Have you found a solution ? – sdabet Jul 13 '23 at 12:43
  • @sdabet Imagine you download an app that uses audio, and plug in your headphones so that you know you won't disturb anyone around you. Then that app suddenly starts using the speaker anyway. Does that sound like something Android should allow? – Jorn Jul 14 '23 at 08:07
  • @Jorn If a permission is requested for that, why not. – sdabet Jul 15 '23 at 07:55
  • Have you seen this answer yet? https://stackoverflow.com/a/71062507/7477675 – anshajkhare Jul 20 '23 at 09:00

0 Answers0