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

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

ลงทะเบียนการโทรด้วยเสียงแบบเสียง

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

KotlinJava
val audioDeviceCallback: AudioDeviceCallback = object : AudioDeviceCallback() {
  override fun onAudioDevicesAdded(addedDevices: Array) {}
  override fun onAudioDevicesRemoved(removedDevices: Array) {}
}

audioManager.registerAudioDeviceCallback(audioDeviceCallback)
final AudioDeviceCallback audioDeviceCallback = new AudioDeviceCallback() {
  @Override
  public void onAudioDevicesAdded(AudioDeviceInfo[] addedDevices) {
  }

  @Override
  public void onAudioDevicesRemoved(AudioDeviceInfo[] removedDevices) {
    // Handle device removal
  }
};

audioManager.registerAudioDeviceCallback(audioDeviceCallback);

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

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

KotlinJava
val currentCommunicationDevice: AudioDeviceInfo? = audioManager.communicationDevice
AudioDeviceInfo currentCommunicationDevice = audioManager.getCommunicationDevice();

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

KotlinJava
val listener =
  OnCommunicationDeviceChangedListener { device -> // Handle changes
    currentCommunicationDevice = device
  }
audioManager.addOnCommunicationDeviceChangedListener(executor, listener)
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 แต่คุณอาจลองค้นหาอุปกรณ์ที่รองรับทั้งหมดและอนุญาตให้ผู้ใช้เลือกได้

KotlinJava

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

// Get list of currently available devices
List

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

หลังจากพบอุปกรณ์ที่เข้ากันได้แล้ว ให้ใช้ setCommunicationDevice เพื่อตั้งค่าอุปกรณ์ที่ต้องการกำหนดเส้นทาง การตรวจสอบผลลัพธ์จะแจ้งให้แอปของคุณทราบว่าโปรแกรมจัดการเสียงกำลังพยายามตั้งค่าอุปกรณ์หรือพบข้อผิดพลาดหรือไม่

KotlinJava

val result = audioManager.setCommunicationDevice(selectedDevice)
if (!result) {
 
// Handle error.
}
// Wait for currentCommunicationDevice to become selectedDevice with a timeout (e.g. 30s)

boolean result = audioManager.setCommunicationDevice(selectedDevice);
if (!result) {
 
// Handle error.
}
// Wait for currentCommunicationDevice to become selectedDevice with a timeout (e.g. 30s)

เมื่อตั้งค่าอุปกรณ์ BLE Audio แล้ว สตรีมเสียงจะมีการกำหนดเส้นทางเสียงที่ถูกต้องเมื่อโทรออก

สิ้นสุดเซสชัน

หลังจากที่แอปโทรหรือเซสชันเสร็จแล้ว ให้ล้างอุปกรณ์การสื่อสารที่ใช้งานอยู่ ซึ่งจะช่วยให้ผู้ใช้ได้รับประสบการณ์ที่ยอดเยี่ยมเมื่อต้องสลับไปมาระหว่างแอปพลิเคชันต่างๆ

KotlinJava

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

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