Skip to content

Instantly share code, notes, and snippets.

@pdesantis
Created June 10, 2021 15:53
Show Gist options
  • Save pdesantis/b1899706ea14045eb345de5584d2a3bf to your computer and use it in GitHub Desktop.
Save pdesantis/b1899706ea14045eb345de5584d2a3bf to your computer and use it in GitHub Desktop.
func getDeviceID(deviceUID: String) -> AudioObjectID? {
var deviceUIDCFString = deviceUID as CFString
var deviceID = kAudioDeviceUnknown
var propertyAddress = AudioObjectPropertyAddress(mSelector: kAudioHardwarePropertyDeviceForUID, mScope: kAudioObjectPropertyScopeGlobal, mElement: kAudioObjectPropertyElementMaster)
let dataSize_CFString = UInt32(MemoryLayout<CFString>.stride)
let dataSize_AudioObjectID = UInt32(MemoryLayout<AudioObjectID>.stride)
// This started giving warnings after upgrading to Swift 5.2
var translation = AudioValueTranslation(
mInputData: &deviceUIDCFString, // Warning: "Inout expression creates a temporary pointer, but argument 'mInputData' should be a pointer that outlives the call to 'init(mInputData:mInputDataSize:mOutputData:mOutputDataSize:)'"
mInputDataSize: dataSize_CFString,
mOutputData: &deviceID, // Warning: "Inout expression creates a temporary pointer, but argument 'mOutputData' should be a pointer that outlives the call to 'init(mInputData:mInputDataSize:mOutputData:mOutputDataSize:)'"
mOutputDataSize: dataSize_AudioObjectID
)
var specifierSize = UInt32(MemoryLayout<AudioValueTranslation>.stride)
let result = AudioObjectGetPropertyData(UInt32(kAudioObjectSystemObject), &propertyAddress, 0, nil, &specifierSize, &translation)
guard result == kAudioHardwareNoError, deviceID != kAudioDeviceUnknown else {
return nil
}
return deviceID
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment