Skip to content

Instantly share code, notes, and snippets.

@moutend
Created December 15, 2021 00:21
Show Gist options
  • Save moutend/8cdc63b5918a9aa25c7bf2889e0ad59f to your computer and use it in GitHub Desktop.
Save moutend/8cdc63b5918a9aa25c7bf2889e0ad59f to your computer and use it in GitHub Desktop.
Print the default sound output device name on macOS 12.0.

Usage

$ swift main.swift
Default output device: MacBook Pro Speakers
import AudioToolbox
func main() {
var address = AudioObjectPropertyAddress(
mSelector: AudioObjectPropertySelector(kAudioHardwarePropertyDefaultOutputDevice),
mScope: AudioObjectPropertyScope(kAudioObjectPropertyScopeGlobal),
mElement: AudioObjectPropertyElement(kAudioObjectPropertyElementMain)
)
var size = UInt32(MemoryLayout<AudioDeviceID>.size)
var deviceID = AudioDeviceID()
// Get device ID.
AudioObjectGetPropertyData(
AudioObjectID(kAudioObjectSystemObject), &address, 0, nil, &size, &deviceID)
// Get output device name.
var deviceNameRef: CFString? = nil
address.mSelector = kAudioObjectPropertyName
size = UInt32(MemoryLayout<CFString?>.size)
AudioObjectGetPropertyData(deviceID, &address, 0, nil, &size, &deviceNameRef)
guard let deviceName = deviceNameRef else {
return
}
print("Default output device: \(deviceName)")
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment