ปรับปรุงการเล่นเสียง

เมื่อคุณพยายามเข้าถึงอุปกรณ์ต่อพ่วงเสียง USB โดยตรงโดยใช้ USB API ปัญหาจะเกิดขึ้น ปัญหาเหล่านี้อาจรวมถึงปัญหาด้านความปลอดภัย การจำกัดการเล่นสื่อจากแอปอื่นๆ และการสูญเสียการปลุก การแจ้งเตือน และเสียงเรียกเข้าผ่านอุปกรณ์ USB

หากต้องการปรับปรุงการเล่นเสียง ให้กำหนดค่าแอตทริบิวต์มิกเซอร์แทน

กำหนดค่าแอตทริบิวต์มิกเซอร์

เมื่อใช้ AudioMixerAttributes API คุณจะกำหนดค่าแอปด้วยแอตทริบิวต์มิกเซอร์ที่ต้องการผ่าน USB ได้

เมื่อการเล่นแอปตรงกับรูปแบบการเข้ารหัส มาสก์ช่อง และอัตราการสุ่มตัวอย่างของแอตทริบิวต์มิกเซอร์ที่ต้องการ ระบบจะแนบการเล่นเข้ากับสตรีมเอาต์พุตเสียงที่กำหนดค่ามิกเซอร์ด้วยแอตทริบิวต์มิกเซอร์ที่ต้องการ

แอปของคุณสามารถสตรีมที่การกำหนดค่าใดก็ได้ไปยังระดับชั้นการจัดการฮาร์ดแวร์โดยตรง (HAL) และไปยังอุปกรณ์ ตราบใดที่อุปกรณ์ USB รองรับการกำหนดค่านั้น

ลักษณะการทำงานของมิกเซอร์ที่อนุญาต 2 รายการใน AudioMixerAttributes คือ DEFAULT และ BIT_PERFECT เมื่อลักษณะการทำงานของมิกเซอร์เป็น DEFAULT แสดงว่ามีการมิกซ์ข้อมูลเสียงจากแหล่งที่มาต่างๆ

เมื่อลักษณะการทำงานของมิกเซอร์เป็น BIT_PERFECT ระบบจะไม่ใช้การมิกซ์เสียง การปรับระดับเสียง หรือเอฟเฟกต์เสียงที่ประมวลผลกับการเล่น ระบบจะส่งข้อมูลตามที่เป็นอยู่ไปยัง HAL และส่งไปยังอุปกรณ์ USB ในที่สุด

การใช้ BIT_PERFECT ช่วยให้คุณสตรีม Direct Stream Digital (DSD) ผ่าน Pulse Code Modulation (PCM) ในอุปกรณ์ที่ทำงานด้วยระบบ Android ได้ ตัวอย่างโค้ดต่อไปนี้แสดงวิธีดำเนินการดังกล่าว

val EXPECTED_FORMAT: AudioFormat = AudioFormat.Builder()
    .setEncoding(AudioFormat.ENCODING_PCM_24BIT_PACKED)
    .setChannelMask(AudioFormat.CHANNEL_OUT_STEREO)
    .setSampleRate(44100)
    .build()

fun startPlayback() {
    // Query all supported mixer attributes
    val mixerAttributesList: List<AudioMixerAttributes?> =
        mAudioManager.getSupportedMixerAttributes(usbDevice)

    // Find the wanted mixer attributes
    val mixerAttributes = mixerAttributesList.stream()
        .filter { mixerAttr: AudioMixerAttributes? ->
            EXPECTED_FORMAT.equals(
                mixerAttr!!.format
            )
        }
        .findAny()
        .orElse(null) ?: return

    // Register a listener to mixer attributes changed
    val listener = MyPreferredMixerAttributesChangedListener()
    mAudioManager.addOnPreferredMixerAttributesChangedListener(
        Executors.newSingleThreadExecutor(), listener
    )

    // Currently, only media usage over USB devices will be allowed
    val attr: AudioAttributes = AudioAttributes.Builder()
        .setUsage(AudioAttributes.USAGE_MEDIA).build()
    // Set preferred mixer attributes
    mAudioManager.setPreferredMixerAttributes(
        attr, usbDevice, mixerAttributes
    )

    // Start playback, note the playback and the audio format must
    // match what is set when calling `setPreferredMixerAttriutes`
    // API.
    val audioTrack = AudioTrack.Builder()
        .setAudioAttributes(attr)
        .setAudioFormat(mixerAttributes!!.format)
        .build()

    // Clear all preferred mixer attributes related stuff when
    // playback task is completed
    mAudioManager.clearPreferredMixerAttributes(attr, usbDevice)
    mAudioManager.removeOnPreferredMixerAttributesChangedListener(listener)
}

private class MyPreferredMixerAttributesChangedListener :
    AudioManager.OnPreferredMixerAttributesChangedListener {
    override fun onPreferredMixerAttributesChanged(
        attributes: AudioAttributes,
        device: AudioDeviceInfo,
        mixerAttributes: AudioMixerAttributes?,
    ) {
        // Do something when preferred mixer attributes changed
    }
}