คู่มือการโทรที่จัดการด้วยตนเองในโปรแกรมจัดการเสียง

คู่มือนี้ครอบคลุมแอปพลิเคชันการสื่อสาร เช่น Voice over Internet Protocol (VoIP) ที่ต้องการจัดการสถานะเสียงและอุปกรณ์ที่ได้ยินด้วยตนเอง

ลงทะเบียนการติดต่อกลับด้วยเสียง

ก่อนอื่น ให้สร้าง AudioDeviceCallback ซึ่งจะแจ้งเตือนแอปเมื่ออุปกรณ์เสียงเชื่อมต่อหรือยกเลิกการเชื่อมต่อจากอุปกรณ์

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

ตรวจหาอุปกรณ์สื่อสารที่ใช้งานอยู่

ตัวจัดการเสียงจะช่วยให้คุณทราบว่าอุปกรณ์สื่อสารใดที่กำลังทำงานอยู่

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

ใช้ getAvailableCommunicationDevices เพื่อดูว่าอุปกรณ์ใดพร้อมใช้งานสำหรับการโทรผ่าน VoIP ใช้ AudioDeviceInfo เพื่อดูว่าอุปกรณ์เป็นชุดหูฟัง BLE หรือไม่ ตัวอย่างนี้จะค้นหาอุปกรณ์แรกที่รองรับ BLE Audio แต่คุณอาจพิจารณาค้นหาอุปกรณ์ที่รองรับทั้งหมดและให้ผู้ใช้เลือกด้วย

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

ตั้งค่าอุปกรณ์สื่อสาร

หลังจากพบอุปกรณ์ที่เข้ากันได้แล้ว ให้ใช้ 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();