735Tesla
735Tesla

Reputation: 3261

OS X Programmatically Set Default Output Device

I have been attempting to change which audio device my computer sends sound to. My end goal is to create a program that can make my laptop output to its built-in speakers even when headphones are plugged into the headphone jack.

I stumbled across this project, but the methods it uses (specifically AudioHardwareSetProperty) are deprecated. It also just doesn't work (it will say it changed the output device, but sound will still go to my headphones).

CoreAudio seems very poorly documented and I could not find ANY code online that did not use that function. I would go with the deprecated function if it did what I wanted, but it doesn't. I'm unsure weather it's broken or just doesn't do what I think it does, but that really doesn't matter in the end.

I attempted to look at the comments on AudioHardwareSetProperty but all I found was this in the discussion section:

Note that the value of the property should not be considered changed until the HAL has called the listeners as many properties values are changed asynchronously. Also note that the same functionality is provided by the function AudioObjectGetPropertyData().

This is obviously not true, since I know for a fact that AudioObjectGetPropertyData is used for getting information about one specific audio device.

Is what I am trying to do possible with CoreAudio?

Upvotes: 8

Views: 2984

Answers (1)

Alexander Vasenin
Alexander Vasenin

Reputation: 13053

With new CoreAudio API introduced in macOS 15 it's very easy:

let system = AudioHardwareSystem.shared
let devices = try system.devices
let newDefaultOutput = ... // Select target device from devices
try system.setDefaultOutputDevice(newDefaultOutput)
try system.setDefaultSoundEffectsDevice(newDefaultOutput) // Optional

Upvotes: 0

Related Questions