4

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

AudioObjectPropertyAddress theAddress = { kAudioHardwarePropertyDefaultInputDevice,
                                          kAudioObjectPropertyScopeGlobal,
                                          kAudioObjectPropertyElementMaster };

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

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

4

2 回答 2

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;
}
于 2012-08-15T13:12:22.190 回答
5

@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
于 2015-02-20T11:02:59.493 回答