fix: use setCommunicationDevice API for audio routing on Android 12+
AudioManager.isSpeakerphoneOn is deprecated since API 31 and silently ignored on modern devices. Switch to setCommunicationDevice() which properly routes call audio to speaker, earpiece, or Bluetooth on Android 12+, with fallback to the legacy API on older versions. https://claude.ai/code/session_014espsysob7MrE8X8e75jFX
This commit is contained in:
@@ -127,7 +127,11 @@ class CallAudioManager(
|
|||||||
fun restoreAudioMode() {
|
fun restoreAudioMode() {
|
||||||
stopBluetoothSco()
|
stopBluetoothSco()
|
||||||
unregisterBluetoothScoReceiver()
|
unregisterBluetoothScoReceiver()
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
||||||
|
audioManager.clearCommunicationDevice()
|
||||||
|
}
|
||||||
audioManager.mode = previousAudioMode
|
audioManager.mode = previousAudioMode
|
||||||
|
@Suppress("DEPRECATION")
|
||||||
audioManager.isSpeakerphoneOn = false
|
audioManager.isSpeakerphoneOn = false
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -189,17 +193,47 @@ class CallAudioManager(
|
|||||||
|
|
||||||
private fun routeToEarpiece() {
|
private fun routeToEarpiece() {
|
||||||
stopBluetoothSco()
|
stopBluetoothSco()
|
||||||
audioManager.isSpeakerphoneOn = false
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
||||||
|
val earpiece =
|
||||||
|
audioManager
|
||||||
|
.availableCommunicationDevices
|
||||||
|
.firstOrNull { it.type == AudioDeviceInfo.TYPE_BUILTIN_EARPIECE }
|
||||||
|
earpiece?.let { audioManager.setCommunicationDevice(it) }
|
||||||
|
} else {
|
||||||
|
@Suppress("DEPRECATION")
|
||||||
|
audioManager.isSpeakerphoneOn = false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun routeToSpeaker() {
|
private fun routeToSpeaker() {
|
||||||
stopBluetoothSco()
|
stopBluetoothSco()
|
||||||
audioManager.isSpeakerphoneOn = true
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
||||||
|
val speaker =
|
||||||
|
audioManager
|
||||||
|
.availableCommunicationDevices
|
||||||
|
.firstOrNull { it.type == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER }
|
||||||
|
speaker?.let { audioManager.setCommunicationDevice(it) }
|
||||||
|
} else {
|
||||||
|
@Suppress("DEPRECATION")
|
||||||
|
audioManager.isSpeakerphoneOn = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun routeToBluetooth() {
|
private fun routeToBluetooth() {
|
||||||
audioManager.isSpeakerphoneOn = false
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
||||||
startBluetoothSco()
|
val btDevice =
|
||||||
|
audioManager
|
||||||
|
.availableCommunicationDevices
|
||||||
|
.firstOrNull {
|
||||||
|
it.type == AudioDeviceInfo.TYPE_BLUETOOTH_SCO ||
|
||||||
|
it.type == AudioDeviceInfo.TYPE_BLE_HEADSET
|
||||||
|
}
|
||||||
|
btDevice?.let { audioManager.setCommunicationDevice(it) }
|
||||||
|
} else {
|
||||||
|
@Suppress("DEPRECATION")
|
||||||
|
audioManager.isSpeakerphoneOn = false
|
||||||
|
startBluetoothSco()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun hasBluetoothDevice(): Boolean =
|
private fun hasBluetoothDevice(): Boolean =
|
||||||
|
|||||||
Reference in New Issue
Block a user