即使我的耳机使用 AudioManager 插入我的设备,我也必须保留音频扬声器,但是当编写此代码时,AudioManager 在 6 秒后自动切换到正常模式。 还有另一种方法可以在 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);
}
}
}