音频管理器自管理通话指南

本指南涵盖希望自行管理其音频和可听设备状态的通信应用,例如网络电话 (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();