WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

SOLVED: How do you specify the audio output device on a Mac in Swift?

Forums > macOS

I have a program that plays an audio stream from the network. Right now, it always sends the audio to the default output device specified in Sound Preferences. I have seen other programs that provide a list of possible audio devices and lets the user select, but I have spent several hours searching and can't find out how to do this.

Also, FWIW, even though the program does send output to the default device, I always see a console printout similar to:

2022-03-05 17:11:14.959506-0500 NetworkIcom[69082:1816765] [plugin] AddInstanceForFactory: No factory registered for id <CFUUID 0x600002ad6340> F8BB1C28-BAE8-11D6-9C31-00039315CD46
2022-03-05 17:11:14.985656-0500 NetworkIcom[69082:1816765] [ddagg]        AggregateDevice.mm:878   couldn't get default input device, ID = 0, err = 0!
2022-03-05 17:11:14.996557-0500 NetworkIcom[69082:1816765]  AudioObjectGetPropertyData: no object with given ID 0
2022-03-05 17:11:15.001375-0500 NetworkIcom[69082:1816765]  AudioObjectGetPropertyData: no object with given ID 0
2022-03-05 17:11:15.001563-0500 NetworkIcom[69082:1816765]  AudioObjectGetPropertyData: no object with given ID 0

Stepping through the code with the debugger, the above lines are printed after I step over the last line below.

    private let engine: AVAudioEngine

    ...

        engine = AVAudioEngine()
        let output = engine.outputNode

   

I found a comment in a thread on the Apple Developer Forum that let me to a soludion.

Apple Developer Forum

The code that worked for me:

        engine = AVAudioEngine()
        let output = engine.outputNode

        // get the low level input audio unit from the engine:
        let outputUnit = output.audioUnit!
        // use core audio low level call to set the input device:
        var outputDeviceID: AudioDeviceID = 51  // replace with actual, dynamic value
        AudioUnitSetProperty(outputUnit,
                             kAudioOutputUnitProperty_CurrentDevice,
                             kAudioUnitScope_Global,
                             0,
                             &outputDeviceID,
                             UInt32(MemoryLayout<AudioDeviceID>.size))

   

Excellent find!

Please mark your own answer as "Correct"

   

Hacking with Swift is sponsored by Emerge

SPONSORED Optimize your app’s startup time, binary size, and overall performance using Emerge’s advanced app optimization and monitoring tools. Reliably measure app size, speed up your app's startup time with Emerge's Launch Booster, and much more. Emerge is actively used by many of the top mobile development teams in the world.

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.