【问题标题】:how to get Audio Device UID to pass into NSSound's setPlaybackDeviceIdentifier:如何获取音频设备 UID 以传递到 NSSound 的设置播放设备标识符:
【发布时间】:2009-12-31 04:52:53
【问题描述】:

如何让音频设备 UID(USB 扬声器)传递到 NSSound 的 setPlaybackDeviceIdentifier: 方法

谢谢

【问题讨论】:

  • 我最近自己也遇到了这个麻烦,最终写了a small package 来解决这个问题。希望有人会觉得它有帮助。

标签: macos core-audio


【解决方案1】:

为避免已弃用的 AudioHardwareGetProperty 和 AudioDeviceGetProperty 调用将它们替换为以下内容:

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);
}

【讨论】:

  • 如果我可以再次投票,我会的。我试图自己解决这个问题,但找不到任何文档。据我所知,这是所有互联网中唯一记录这一点的地方。
  • 而不是使用char[64] 作为易碎的字符串,您可以使用属性的CFString 变体:例如kAudioDevicePropertyDeviceNameCFString.
  • 在 2019 年仍然有意义!
【解决方案2】:

好吧,我自己搞定了……

theCFString 将包含设备 UID

UInt32          theSize;
char            theString[kMaxStringSize];
UInt32          theNumberDevices;
AudioDeviceID   *theDeviceList = NULL;
UInt32          theDeviceIndex;
CFStringRef     theCFString     = NULL;
OSStatus        theStatus = noErr;

// this is our driver
const char      *nameString = "Burr-Brown Japan PCM2702";
const char      *manufacturerString = "Burr-Brown Japan";



// device list size
theSize = 0;
theStatus = AudioHardwareGetPropertyInfo(kAudioHardwarePropertyDevices, &theSize, NULL);


theNumberDevices = theSize / sizeof(AudioDeviceID);

// allocate the device list
theDeviceList = (AudioDeviceID*)malloc(theNumberDevices * sizeof(AudioDeviceID));

// get the device list
theSize = theNumberDevices * sizeof(AudioDeviceID);
theStatus = AudioHardwareGetProperty(kAudioHardwarePropertyDevices, &theSize, theDeviceList);

// iterate through the device list, find our device and return the UID
for(theDeviceIndex = 0; theDeviceIndex < theNumberDevices; ++theDeviceIndex)
{
    // get name
    theSize = kMaxStringSize;
    theStatus = AudioDeviceGetProperty(theDeviceList[theDeviceIndex],
                                       0, 0, kAudioDevicePropertyDeviceName, &theSize, theString);

    NSLog(@"%s",theString);


    // is it me?
    if (strncmp(theString, nameString, strlen(nameString)) == 0) {

        // get manufacturer
        theSize = kMaxStringSize;
        theStatus = AudioDeviceGetProperty(theDeviceList[theDeviceIndex], 0, 0,
                                           kAudioDevicePropertyDeviceManufacturer, &theSize, theString);

        NSLog(@"%s",theString);
        // is it really me?
        if (strncmp(theString, manufacturerString, strlen(manufacturerString)) == 0) {
            // get device UID
            theSize = sizeof(CFStringRef);
            theStatus = AudioDeviceGetProperty(theDeviceList[theDeviceIndex],
                                               0, 0, kAudioDevicePropertyDeviceUID, &theSize, &theCFString);
            NSLog(@"%s",theCFString);



            break;
        }
    }
}

【讨论】:

    【解决方案3】:

    AudioHardwareGetProperty 在雪豹中已弃用。

    【讨论】:

    • 由于不推荐使用 AudioHardwareGetProperty,因此必须使用 AudioObjectGetPropertyData。最好避免使用已弃用的 API。
    猜你喜欢
    • 2020-01-28
    • 1970-01-01
    • 2016-01-09
    • 2013-02-09
    • 1970-01-01
    • 2020-10-06
    • 1970-01-01
    • 1970-01-01
    • 2011-12-21
    相关资源
    最近更新 更多