如何将音频设备 UID 传递到 NSSound 的 setPlaybackDeviceIdentifier 中:

发布于 2024-08-16 02:58:39 字数 80 浏览 5 评论 0原文

我怎样才能将音频设备UID(USB扬声器)传递到NSSound的setPlaybackDeviceIdentifier:方法

谢谢

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

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

装迷糊 2024-08-23 02:58:39

为了避免已弃用的 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);
}

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);
}
云胡 2024-08-23 02:58:39

好的,我自己得到了...

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

ok i got it myself...

the theCFString will contain the device 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;
        }
    }
}
沒落の蓅哖 2024-08-23 02:58:39

AudioHardwareGetProperty 在雪豹中已弃用。

AudioHardwareGetProperty is deprecated in snow leopard.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文