TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

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

3      

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

3      

Excellent find!

Please mark your own answer as "Correct"

3      

Hacking with Swift is sponsored by Superwall.

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.