Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
789 views
in Technique[技术] by (71.8m points)

macos - how to get Audio Device UID to pass into NSSound's setPlaybackDeviceIdentifier:

How can i get audio device UID (USB speaker) to pass into NSSound's setPlaybackDeviceIdentifier: method

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To avoid the deprecated AudioHardwareGetProperty and AudioDeviceGetProperty calls replace them with something like this:

AudioObjectPropertyAddress  propertyAddress;
AudioObjectID               *deviceIDs;
UInt32                      propertySize;
NSInteger                   numDevices;

propertyAddress.mSelector = kAudioHardwarePropertyDevices;
propertyAddress.mScope = kAudioObjectPropertyScopeGlobal;
propertyAddress.mElement = kAudioObjectPropertyElementMaster;
if (AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &propertyAddress, 0, NULL, &propertySize) == noErr) {
    numDevices = propertySize / sizeof(AudioDeviceID);
    deviceIDs = (AudioDeviceID *)calloc(numDevices, sizeof(AudioDeviceID));

    if (AudioObjectGetPropertyData(kAudioObjectSystemObject, &propertyAddress, 0, NULL, &propertySize, deviceIDs) == noErr) {
        AudioObjectPropertyAddress      deviceAddress;
        char                            deviceName[64];
        char                            manufacturerName[64];

        for (NSInteger idx=0; idx<numDevices; idx++) {
            propertySize = sizeof(deviceName);
            deviceAddress.mSelector = kAudioDevicePropertyDeviceName;
            deviceAddress.mScope = kAudioObjectPropertyScopeGlobal;
            deviceAddress.mElement = kAudioObjectPropertyElementMaster;
            if (AudioObjectGetPropertyData(deviceIDs[idx], &deviceAddress, 0, NULL, &propertySize, deviceName) == noErr) {
                propertySize = sizeof(manufacturerName);
                deviceAddress.mSelector = kAudioDevicePropertyDeviceManufacturer;
                deviceAddress.mScope = kAudioObjectPropertyScopeGlobal;
                deviceAddress.mElement = kAudioObjectPropertyElementMaster;
                if (AudioObjectGetPropertyData(deviceIDs[idx], &deviceAddress, 0, NULL, &propertySize, manufacturerName) == noErr) {
                    CFStringRef     uidString;

                    propertySize = sizeof(uidString);
                    deviceAddress.mSelector = kAudioDevicePropertyDeviceUID;
                    deviceAddress.mScope = kAudioObjectPropertyScopeGlobal;
                    deviceAddress.mElement = kAudioObjectPropertyElementMaster;
                    if (AudioObjectGetPropertyData(deviceIDs[idx], &deviceAddress, 0, NULL, &propertySize, &uidString) == noErr) {
                        NSLog(@"device %s by %s id %@", deviceName, manufacturerName, uidString);

                        CFRelease(uidString);
                    }
                }
            }
        }
    }

    free(deviceIDs);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...