Audio Output for Calls (Android)
To enable seamless audio output switching within your application, you'll want to integrate Android's Telecom framework. Telecom empowers your app to manage call services efficiently, ensuring a smooth user experience. Please refer to Android's official documentation on Telecom here.
Integrating Telecom
For comprehensive guidance on integrating Telecom framework into your project, please refer to Telecom Integration.
Observing Audio Output Changes
You can subscribe to delegate method onCallAudioStateChanged or onCallEndpointChanged to observe changes in the audio output.
Here's how you can do it:
For Android API 33 and Below
override fun onCallAudioStateChanged(state: CallAudioState?) {
state ?: return
// highlight-next-line
println("New call route: ${state.route}")
}
For Android API 34 and Above
override fun onCallEndpointChanged(callEndpoint: CallEndpoint) {
// highlight-next-line
println("New call endpoint: ${callEndpoint}")
}
You can also override the audio output source using setAudioRoute or requestCallEndpointChange :
For Android API 33 and Below
call.setAudioRoute(CallAudioState.ROUTE_BLUETOOTH)
For Android API 34 and Above
override fun onAvailableCallEndpointsChanged(availableEndpoints: List<CallEndpoint>) {
for (endpoint in availableEndpoints) {
if (endpoint.endpointType == CallEndpoint.ENDPOINT_TYPE_BLUETOOTH) {
requestCallEndpointChange(
endpoint,
Executor { it.run() },
object : OutcomeReceiver<Void, CallEndpointException> {
override fun onResult(result: Void?) {
// Successfully switched to Bluetooth
}
override fun onError(error: CallEndpointException) {
// Handle error during endpoint change
}
}
)
break
}
}
}
We recommend avoiding using those methods to route audio output and rely only on the Telecom built-in UI for a better experience.