AVAudioEngine is using the wrong format when a Bluetooth headset is connected

I have a pair of bluetooth headphones with mic input. The microphone is not used, but when installed, the input and output are forced to 8000 kHz.

My instance of AVAudioEngine connects to the headset at 8000 kHz, unless I go into system preferences and indicate that I don't want to use the headset for input (which must be done every time the headset is plugged in).

I have noticed that other applications can play at the expected 44100 kHz without issue. There are no input nodes in my AVAudioEngine graphics.

How can I make AVAudioEngine my preferred connection with a reasonable sample rate?

+3


source to share


1 answer


After my unfortunate generosity, I wrote an Apple DTS and received a great response (including the sample code below, which I translated from Objective-C).

The function below connects to the default audio device in output-only mode, not inout / output mode, which is the default. Remember to call it before starting the engine!



func setOutputDeviceFor(_ engine: AVAudioEngine) -> Bool {
    var addr = AudioObjectPropertyAddress(
        mSelector: kAudioHardwarePropertyDefaultOutputDevice,
        mScope: kAudioObjectPropertyScopeGlobal,
        mElement: kAudioObjectPropertyElementMaster)

    var deviceID: AudioObjectID = 0
    var size = UInt32(MemoryLayout.size(ofValue: deviceID))
    let err = AudioObjectGetPropertyData(
        AudioObjectID(kAudioObjectSystemObject),
        &addr,
        0,
        nil,
        &size,
        &deviceID)

    if (noErr == err && kAudioDeviceUnknown != deviceID) {
        do {
            try engine.outputNode.auAudioUnit.setDeviceID(deviceID)
        } catch {
            print(error)
            return false
        }
        return true
    } else {
        print("ERROR: couldn't get default output device, ID = \(deviceID), err = \(err)")
        return false
    }
}

      

+2


source







All Articles