从Core Audio获取内置输出

时间:2012-07-05 15:48:28

标签: objective-c macos core-audio

是否可以可靠地获取Mac内置输出的AudioDeviceID?我尝试使用kAudioHardwarePropertyDefaultOutputDevice来获取当前选择的输出设备,但这可以是任何旧设备,具体取决于用户在系统首选项中选择的内容 - 我只需要内置扬声器的ID。

AudioObjectPropertyAddress theAddress = { kAudioHardwarePropertyDefaultInputDevice,
                                          kAudioObjectPropertyScopeGlobal,
                                          kAudioObjectPropertyElementMaster };

verify_noerr (AudioObjectGetPropertyData(kAudioObjectSystemObject,
                                         &theAddress,
                                         0,
                                         NULL,
                                         &propsize,
                                         &inputDevice));

目前我正在获取所有设备的列表,并通过name == "Built-in Output"检查设备的名称字符串。这适用于我的机器,但似乎不是一个非常强大的解决方案!有什么像kAudioHardwarePropertyBuiltInOutputDevice吗?

2 个答案:

答案 0 :(得分:5)

经过几天的研究,我能提出的最佳答案是寻找你提到的内置输出,但是也添加了制造商检查,如下所示......

- (NSString *)getBuiltInOutputDeviceUID
{
    NSString *deviceUID = @"";

    AudioObjectPropertyAddress aopa;
    aopa.mSelector = kAudioHardwarePropertyDevices;
    aopa.mScope = kAudioObjectPropertyScopeGlobal;
    aopa.mElement = kAudioObjectPropertyElementMaster;

    UInt32 propSize;
    OSStatus error = AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &aopa, 0, NULL, &propSize);
    if (error == noErr) {
        int deviceCount = propSize / sizeof(AudioDeviceID);
        AudioDeviceID *audioDevices = (AudioDeviceID *)malloc(propSize);
        error = AudioObjectGetPropertyData(kAudioObjectSystemObject, &aopa, 0, NULL, &propSize, audioDevices);
        if (error == noErr) {
            UInt32 propSize = sizeof(CFStringRef);
            for(int i = 1; i <= deviceCount; i++) {
                NSString *result;
                aopa.mSelector = kAudioDevicePropertyDeviceManufacturerCFString;
                error = AudioObjectGetPropertyData(audioDevices[i], &aopa, 0, NULL, &propSize, &result);
                if (error != noErr || ![result isEqualToString:@"Apple Inc."]) {
                    continue;
                }
                aopa.mSelector = kAudioDevicePropertyDeviceNameCFString;
                error = AudioObjectGetPropertyData(audioDevices[i], &aopa, 0, NULL, &propSize, &result);
                if (error != noErr || ![result isEqualToString:@"Built-in Output"]) {
                    continue;
                }
                aopa.mSelector = kAudioDevicePropertyDeviceUID;
                error = AudioObjectGetPropertyData(audioDevices[i], &aopa, 0, NULL, &propSize, &result);
                if (error == noErr) {
                    deviceUID = result;
                    break;
                }
            }
        }
        free(audioDevices);
    }
    return deviceUID;
}

答案 1 :(得分:2)

@ Equinox2000的答案正在发挥作用,但有一种方法可以避免繁重的字符串比较。只需使用AudioObject选择器获取另一个kAudioDevicePropertyTransportType的属性。

aopa.mSelector = kAudioDevicePropertyTransportType;
aopa.mScope = kAudioObjectPropertyScopeGlobal;
UInt32 size = sizeof(UInt32);
UInt32 transportType = 0;
OSStatus status = AudioObjectGetPropertyData(device.deviceId, &address, 0, NULL, &size, & transportType);
if (transportType == kAudioDeviceTransportTypeBuiltIn) // that's all the checks
    // do something