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 f916e4e3d4
commit 5d3de494d7
4 changed files with 62 additions and 17 deletions

View File

@@ -8,6 +8,9 @@
package io.element.android.features.call.impl.ui
import android.annotation.SuppressLint
import android.content.Context
import android.media.AudioDeviceCallback
import android.media.AudioDeviceInfo
import android.media.AudioManager
import android.util.Log
import android.view.ViewGroup
@@ -22,6 +25,10 @@ import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalInspectionMode
@@ -151,15 +158,11 @@ private fun CallWebView(
Text("WebView - can't be previewed")
}
} else {
var audioDeviceCallback: AudioDeviceCallback? by remember { mutableStateOf(null) }
AndroidView(
modifier = modifier,
factory = { context ->
// Set 'voice call' mode so volume keys actually control the call volume
val audioManager = context.getSystemService<AudioManager>()
audioManager?.mode = AudioManager.MODE_IN_COMMUNICATION
audioManager?.enableExternalAudioDevice()
audioDeviceCallback = context.setupAudioConfiguration()
WebView(context).apply {
onWebViewCreate(this)
setup(userAgent, onPermissionsRequest)
@@ -172,16 +175,40 @@ private fun CallWebView(
},
onRelease = { webView ->
// Reset audio mode
val audioManager = webView.context.getSystemService<AudioManager>()
audioManager?.disableExternalAudioDevice()
audioManager?.mode = AudioManager.MODE_NORMAL
webView.context.releaseAudioConfiguration(audioDeviceCallback)
webView.destroy()
}
)
}
}
private fun Context.setupAudioConfiguration(): AudioDeviceCallback? {
val audioManager = getSystemService<AudioManager>() ?: return null
// Set 'voice call' mode so volume keys actually control the call volume
audioManager.mode = AudioManager.MODE_IN_COMMUNICATION
audioManager.enableExternalAudioDevice()
return object : AudioDeviceCallback() {
override fun onAudioDevicesAdded(addedDevices: Array<out AudioDeviceInfo>?) {
Timber.d("Audio devices added")
audioManager.enableExternalAudioDevice()
}
override fun onAudioDevicesRemoved(removedDevices: Array<out AudioDeviceInfo>?) {
Timber.d("Audio devices removed")
audioManager.enableExternalAudioDevice()
}
}.also {
audioManager.registerAudioDeviceCallback(it, null)
}
}
private fun Context.releaseAudioConfiguration(audioDeviceCallback: AudioDeviceCallback?) {
val audioManager = getSystemService<AudioManager>() ?: return
audioManager.unregisterAudioDeviceCallback(audioDeviceCallback)
audioManager.disableExternalAudioDevice()
audioManager.mode = AudioManager.MODE_NORMAL
}
@SuppressLint("SetJavaScriptEnabled")
private fun WebView.setup(
userAgent: String,

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")