音訊管理工具自行管理通話指南

本指南涵蓋通訊應用程式 (例如透過網際網路通訊協定 (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 Audio 的裝置,不過您也可以考慮找出所有支援的裝置,並讓使用者自行選擇。

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 Audio 裝置後,撥打電話時,音訊串流會正確傳送音訊。

結束練習

應用程式結束通話或工作階段後,請清除使用中的通訊裝置。以確保使用者在不同應用程式之間移動時能獲得良好體驗。

Kotlin


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

Java


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