从核心音频获取内置输出

Dan*_*day 3 macos objective-c core-audio

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

AudioObjectPropertyAddress theAddress = { kAudioHardwarePropertyDefaultInputDevice,
                                          kAudioObjectPropertyScopeGlobal,
                                          kAudioObjectPropertyElementMaster };

verify_noerr (AudioObjectGetPropertyData(kAudioObjectSystemObject,
                                         &theAddress,
                                         0,
                                         NULL,
                                         &propsize,
                                         &inputDevice));
Run Code Online (Sandbox Code Playgroud)

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

Equ*_*000 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;
}
Run Code Online (Sandbox Code Playgroud)


Ast*_*ria 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
Run Code Online (Sandbox Code Playgroud)