Fix audio output selection for Element Call (#4602)

* Fix audio output selection.

* Ensure that Element Call audio output uses a new connected device, even during a call.
Also add a few logs.

* Extract functions.

* Add more log and protect from crash.

* Revert formatting change

* Update screenshots

---------

Co-authored-by: ElementBot <android@element.io>
This commit is contained in:
Benoit Marty 2025-04-17 10:36:42 +02:00 committed by GitHub
parent 7ed362b9db
commit be1c9b793b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 62 additions and 17 deletions

View file

@ -10,6 +10,8 @@ package io.element.android.libraries.androidutils.compat
import android.media.AudioDeviceInfo
import android.media.AudioManager
import android.os.Build
import io.element.android.libraries.core.data.tryOrNull
import timber.log.Timber
fun AudioManager.enableExternalAudioDevice() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
@ -30,10 +32,26 @@ fun AudioManager.enableExternalAudioDevice() {
AudioDeviceInfo.TYPE_BUILTIN_EARPIECE,
)
val devices = availableCommunicationDevices
val selectedDevice = devices.find {
wantedDeviceTypes.contains(it.type)
val selectedDevice = devices.minByOrNull {
wantedDeviceTypes.indexOf(it.type).let { index ->
// If the device type is not in the wantedDeviceTypes list, we give it a low priority
if (index == -1) Int.MAX_VALUE else index
}
}
selectedDevice?.let { device ->
Timber.d("Audio device selected, type: ${device.type}")
tryOrNull(
onError = { failure ->
Timber.e(failure, "Audio: exception when setting communication device")
}
) {
setCommunicationDevice(device).also {
if (!it) {
Timber.w("Audio: unable to set the communication device")
}
}
}
}
selectedDevice?.let { setCommunicationDevice(it) }
} else {
// If we don't have access to the new APIs, use the deprecated ones
@Suppress("DEPRECATION")