Hướng dẫn này đề cập đến các ứng dụng liên lạc, chẳng hạn như Giao thức thoại qua Internet (VoIP), muốn tự quản lý trạng thái thiết bị âm thanh và thiết bị nghe.
Đăng ký lệnh gọi lại âm thanh
Trước tiên, hãy tạo một AudioDeviceCallback để thông báo cho ứng dụng của bạn khi thiết bị âm thanh kết nối hoặc ngắt kết nối khỏi thiết bị.
Kotlin
val audioDeviceCallback: AudioDeviceCallback = object : AudioDeviceCallback() {
override fun onAudioDevicesAdded(addedDevices: Array<AudioDeviceInfo>) {}
override fun onAudioDevicesRemoved(removedDevices: Array<AudioDeviceInfo>) {}
}
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);
Kiểm tra thiết bị liên lạc đang hoạt động
Trình quản lý âm thanh cho bạn biết thiết bị liên lạc nào đang hoạt động.
Kotlin
val currentCommunicationDevice: AudioDeviceInfo? = audioManager.communicationDevice
Java
AudioDeviceInfo currentCommunicationDevice = audioManager.getCommunicationDevice();
Việc theo dõi thời điểm thiết bị liên lạc thay đổi sẽ giúp ứng dụng của bạn biết thời điểm áp dụng định tuyến và thiết bị bạn chọn đang hoạt động.
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);
Tìm thiết bị âm thanh BLE
Sử dụng getAvailableCommunicationDevices để xem những thiết bị nào có thể dùng cho cuộc gọi VoIP. Sử dụng AudioDeviceInfo để xem thiết bị có phải là tai nghe BLE hay không.
Ví dụ này tìm thiết bị đầu tiên hỗ trợ BLE Audio, nhưng bạn cũng có thể cân nhắc việc tìm tất cả thiết bị được hỗ trợ và cho phép người dùng chọn.
Kotlin
// Get list of currently available devices
val devices = audioManager.availableCommunicationDevices
// User choose one of the devices, for example, 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<AudioDeviceInfo> 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;
}
}
Đặt thiết bị liên lạc
Sau khi tìm thấy một thiết bị tương thích, hãy sử dụng setCommunicationDevice để đặt thiết bị mà bạn muốn định tuyến đến. Việc kiểm tra kết quả sẽ thông báo cho ứng dụng của bạn biết liệu trình quản lý âm thanh có đang cố gắng thiết lập thiết bị hay không hoặc liệu trình quản lý âm thanh có gặp lỗi hay không.
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)
Sau khi bạn đặt thiết bị âm thanh BLE, luồng âm thanh sẽ sử dụng đúng định tuyến âm thanh khi bạn thực hiện cuộc gọi.
Kết thúc phiên
Sau khi ứng dụng của bạn kết thúc cuộc gọi hoặc phiên, hãy xoá thiết bị liên lạc đang hoạt động. Điều này giúp đảm bảo người dùng có trải nghiệm tuyệt vời khi di chuyển giữa các ứng dụng khác nhau.
Kotlin
// Clear the selection, ready for next call
audioManager.clearCommunicationDevice()
Java
// Clear the selection, ready for next call
audioManager.clearCommunicationDevice();