$ swift main.swift
Default output device: MacBook Pro Speakers
Created
December 15, 2021 00:21
-
-
Save moutend/8cdc63b5918a9aa25c7bf2889e0ad59f to your computer and use it in GitHub Desktop.
Print the default sound output device name on macOS 12.0.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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