Hướng dẫn cuộc gọi tự quản lý trong Trình quản lý âm thanh

Hướng dẫn này đề cập đến các ứng dụng giao tiếp như Giao thức thoại qua giao thức Internet (VoIP) mà bạn muốn tự quản lý trạng thái âm thanh và trạng thái của thiết bị nghe được.

Đă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) {}
  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);

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 hiện đang hoạt động.

Kotlin

val currentCommunicationDevice: AudioDeviceInfo? = audioManager.communicationDevice

Java

AudioDeviceInfo currentCommunicationDevice = audioManager.getCommunicationDevice();

Việc lắng nghe thời điểm một thiết bị liên lạc đã thay đổi sẽ cho ứng dụng của bạn biết khi nào chế độ định tuyến được áp dụng và thiết bị mà 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 getAvailableCommuncationDevices() để xem những thiết bị nào có thể thực hiện cuộc gọi VoIP. Sử dụng AudioDeviceInfo để xem thiết bị có phải là tai nghe BLE hay không. Trong ví dụ này, chúng tôi sẽ tìm thiết bị đầu tiên hỗ trợ Âm thanh BLE, 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 lựa chọn.

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;
    }
}

Thiết lập thiết bị liên lạc

Sau khi bạn tìm thấy một thiết bị tương thích, hãy dùng setCommunicationDevice để đặt thiết bị mà bạn muốn chuyể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 cài đặt thiết bị hay đã gặp lỗi.

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)

Giờ đây, bạn đã thiết lập xong thiết bị Âm thanh BLE. Khi bạn gọi điện, luồng âm thanh sẽ được định tuyến âm thanh chính xác.

Kết thúc phiên

Sau khi ứng dụng của bạn kết thúc một 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.

Kotlin


// Clear the selection, ready for next call
audioManager.clearCommunicationDevice()

Java


// Clear the selection, ready for next call
audioManager.clearCommunicationDevice();