-
-
Save glaurent/b4e9a2a1bc5223977df428e03d465560 to your computer and use it in GitHub Desktop.
List CoreAudio devices in Swift
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 Foundation | |
import CoreAudio | |
var propertyAddress = AudioObjectPropertyAddress(mSelector: kAudioHardwarePropertyDevices, mScope: kAudioObjectPropertyScopeGlobal, mElement: kAudioObjectPropertyElementMaster) | |
var propertySize:UInt32 = 0 | |
if AudioObjectGetPropertyDataSize(AudioObjectID(kAudioObjectSystemObject), &propertyAddress, 0, nil, &propertySize) == noErr { | |
let numDevices = Int(propertySize) / MemoryLayout<AudioDeviceID>.size | |
var deviceIDs = [AudioDeviceID](repeating: 0, count: numDevices) | |
if AudioObjectGetPropertyData(AudioObjectID(kAudioObjectSystemObject), &propertyAddress, 0, nil, &propertySize, &deviceIDs) == noErr { | |
var deviceAddress = AudioObjectPropertyAddress() | |
var deviceNameCString = [CChar](repeating:0, count: 64) | |
var manufacturerNameCString = [CChar](repeating:0, count: 64) | |
for idx in (0..<numDevices) { | |
// print("\(deviceIDs[idx])\n") | |
propertySize = UInt32(MemoryLayout<CChar>.size * 64) | |
deviceAddress.mSelector = kAudioDevicePropertyDeviceName | |
deviceAddress.mScope = kAudioObjectPropertyScopeGlobal | |
deviceAddress.mElement = kAudioObjectPropertyElementMaster | |
if AudioObjectGetPropertyData(deviceIDs[idx], &deviceAddress, 0, nil, &propertySize, &deviceNameCString) == noErr { | |
let deviceName = String(cString:deviceNameCString) | |
propertySize = UInt32(MemoryLayout<CChar>.size * 64) | |
deviceAddress.mSelector = kAudioDevicePropertyDeviceManufacturer | |
deviceAddress.mScope = kAudioObjectPropertyScopeGlobal | |
deviceAddress.mElement = kAudioObjectPropertyElementMaster | |
if (AudioObjectGetPropertyData(deviceIDs[idx], &deviceAddress, 0, nil, &propertySize, &manufacturerNameCString) == noErr) { | |
let manufacturerName = String(cString:manufacturerNameCString) | |
var uidString = "" as CFString | |
propertySize = UInt32(MemoryLayout.size(ofValue: uidString)) | |
deviceAddress.mSelector = kAudioDevicePropertyDeviceUID | |
deviceAddress.mScope = kAudioObjectPropertyScopeGlobal | |
deviceAddress.mElement = kAudioObjectPropertyElementMaster | |
if (AudioObjectGetPropertyData(deviceIDs[idx], &deviceAddress, 0, nil, &propertySize, &uidString) == noErr) { | |
print("device \(deviceName)\nby \(manufacturerName)\nid \(uidString)\n\n") | |
} | |
} | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
airplay device missing