本指南介绍了一些通信应用,例如希望自行管理音频和可听设备状态的通信应用,例如互联网语音协议 (VoIP)。
注册音频回调
首先,创建一个 AudioDeviceCallback
,它会在音频设备与设备连接或断开连接时通知您的应用。
Kotlin
val audioDeviceCallback: AudioDeviceCallback = object : AudioDeviceCallback() { override fun onAudioDevicesAdded(addedDevices: Array) {} override fun onAudioDevicesRemoved(removedDevices: Array ) {} } audioManager.registerAudioDeviceCallback(audioDeviceCallback)
Java
final AudioDeviceCallback audioDeviceCallback = new AudioDeviceCallback() { @Override public void onAudioDevicesAdded(AudioDeviceInfo[] addedDevices) { } @Override public void onAudioDevicesRemoved(AudioDeviceInfo[] removedDevices) { // Handle device removal } }; audioManager.registerAudioDeviceCallback(audioDeviceCallback);
检查活跃的通信设备
音频管理器可让您了解当前处于活动状态的通信设备。
Kotlin
val currentCommunicationDevice: AudioDeviceInfo? = audioManager.communicationDevice
Java
AudioDeviceInfo currentCommunicationDevice = audioManager.getCommunicationDevice();
通过监听通信设备何时发生变化,您的应用可了解路由何时应用以及您选择的设备处于活动状态。
Kotlin
val listener = OnCommunicationDeviceChangedListener { device -> // Handle changes currentCommunicationDevice = device } audioManager.addOnCommunicationDeviceChangedListener(executor, listener)
Java
AudioManager.OnCommunicationDeviceChangedListener listener = new AudioManager.OnCommunicationDeviceChangedListener() { @Override void onCommunicationDeviceChanged(AudioDeviceInfo device) { // Handle changes currentCommunicationDevice = device; } }; AudioManager.addOnCommunicationDeviceChangedListener(executor, listener);
查找 BLE 音频设备
使用 getAvailableCommuncationDevices()
查看哪些设备支持 VoIP 通话。使用 AudioDeviceInfo
查看设备是否为 BLE 耳机。本示例寻找的是首款支持 BLE 音频的设备,但您也可以考虑查找所有受支持的设备并允许用户进行选择。
Kotlin
// Get list of currently available devices
val devices = audioManager.availableCommunicationDevices
// User choose one of the devices, let's say, TYPE_BLE_HEADSET
val userSelectedDeviceType = AudioDeviceInfo.TYPE_BLE_HEADSET
//for the device from the list
var selectedDevice: AudioDeviceInfo? = null
for (device in devices) {
if (device.type == userSelectedDeviceType) {
selectedDevice = device
break
}
}
Java
// Get list of currently available devices
List devices = audioManager.getAvailableCommunicationDevices();
// User choose one of the devices, for example, TYPE_BLE_HEADSET
int userSelectedDeviceType = AudioDeviceInfo.TYPE_BLE_HEADSET;
// Filter for the device from the list
AudioDeviceInfo selectedDevice = null;
for (AudioDeviceInfo device : devices) {
if (device.getType() == userSelectedDeviceType) {
selectedDevice = device;
break;
}
}
设置通信设备
找到兼容的设备后,使用 setCommunicationDevice
设置您要路由到的设备。查看结果会告知您的应用,音频管理器是否正在尝试设置设备,或者是否遇到了错误。
Kotlin
val result = audioManager.setCommunicationDevice(selectedDevice)
if (!result) {
// Handle error.
}
// Wait for currentCommunicationDevice to become selectedDevice with a timeout (e.g. 30s)
Java
boolean result = audioManager.setCommunicationDevice(selectedDevice);
if (!result) {
// Handle error.
}
// Wait for currentCommunicationDevice to become selectedDevice with a timeout (e.g. 30s)
现在,BLE 音频设备已设置完毕,在拨打电话时,音频流将拥有正确的音频路由。
结束会话
您的应用完成通话或会话后,清除活跃的通信设备。这有助于确保用户在不同的应用之间切换时能够获得良好的体验。
Kotlin
// Clear the selection, ready for next call
audioManager.clearCommunicationDevice()
Java
// Clear the selection, ready for next call
audioManager.clearCommunicationDevice();