2017-09-06 1 views
0

UI 때문에 출력 장치가 어떤 장치인지 알기 위해 노력하고 있습니다. 예를 들어 오디오가 기본적으로 헤드폰을 통해 이동하는 경우 헤드폰 아이콘을 표시해야합니다.출력 장치 유형 (헤드폰 등)

이 정보를 얻으려면 AudioObjectGetPropertyData()kAudioDevicePropertyDataSource을 사용하고 있습니다. 이 경우 내부 스피커 용 'ispk', 헤드폰 용 'hdpn' 등이 반환됩니다.

헤드폰을 연결할 외부 USB-C 허브를 사용하면 코드가 작동하지 않습니다. 함수 호출은 2003332927 오류 (예 : 'who?')를 반환합니다.

내가 얻을 수있는 유일한 정보는 UID가 AppleUSBAudioEngine:Burr-Brown from TI:USB audio CODEC:14412000:2이고, 이름이 USB audio CODEC이고 제조업체가 Burr-Brown from TI 인 것입니다.

더 유용한 정보를 얻을 수 있는지 알고 계십니까?

static NSString *getDataSourceName(UInt32 dataSource) 
{ 
    switch (dataSource) 
    { 
     case 'ispk': 
      return @"internal speaker"; 
     case 'espk': 
      return @"external speaker"; 
     case 'hdpn': 
      return @"headphones"; 
     default: 
      return [NSString stringWithFormat:@"unknown type %d", dataSource]; 
    } 
} 

static void printDefaultOutputDeviceType() 
{ 
    // Get the default output device. 
    AudioDeviceID deviceID; 
    UInt32 defaultOutputPropSize = sizeof(AudioDeviceID); 
    AudioObjectPropertyAddress defaultOutputAddress = { 
     kAudioHardwarePropertyDefaultOutputDevice, 
     kAudioObjectPropertyScopeGlobal, 
     kAudioObjectPropertyElementMaster, 
    }; 
    OSStatus status = AudioObjectGetPropertyData(kAudioObjectSystemObject, 
               &defaultOutputAddress, 
               0, 
               NULL, 
               &defaultOutputPropSize, 
               &deviceID); 
    NSCAssert(status == noErr, @"Cannot get default output device: %d", status); 

    // Get the data source type for the output device. 
    AudioObjectPropertyAddress dataSourceAddress = { 
     kAudioDevicePropertyDataSource, 
     kAudioObjectPropertyScopeOutput, 
     kAudioObjectPropertyElementMaster, 
    }; 
    UInt32 dataSource; 
    UInt32 dataSourcePropSize = sizeof(dataSource); 
    status = AudioObjectGetPropertyData(deviceID, 
             &dataSourceAddress, 
             0, 
             NULL, 
             &dataSourcePropSize, 
             &dataSource); 
    if (status == noErr) { 
     NSLog(@"Audio device with ID %d is: %@", 
       deviceID, 
       getDataSourceName(dataSource)); 
    } 
    else { 
     NSLog(@"Cannot get type for device with ID %d: %d", 
       deviceID, 
       status); 
    } 
} 

답변

0

내가 답을 발견

이 내 테스트 코드입니다. kAudioDevicePropertyTransportType 내가 원했던 것입니다.

Xcode의 열거 형을 Cmd로 클릭하면 잘못된 AudioHarwareDeprecated.h이 올바른 AudioHardwareBase.h 대신 표시되어 일부 열거 형 값을 볼 수 있지만 원하는 것은 표시되지 않습니다.

#import <Foundation/Foundation.h> 
#import <CoreAudio/CoreAudio.h> 


static void logMessage(NSString *format, ...) 
{ 
    va_list args; 
    va_start(args, format); 
    NSMutableString *formattedString = [[NSMutableString alloc] initWithFormat:format 
                    arguments:args]; 
    va_end(args); 

    if (![formattedString hasSuffix:@"\n"]) { 
     [formattedString appendString:@"\n"]; 
    } 

    NSData *formattedData = [formattedString dataUsingEncoding:NSUTF8StringEncoding]; 
    [NSFileHandle.fileHandleWithStandardOutput writeData:formattedData]; 
} 

/* 
* Format a 32-bit code (for instance OSStatus) into a string. 
*/ 
static char *codeToString(UInt32 code) 
{ 
    static char str[5] = { '\0' }; 
    UInt32 swapped = CFSwapInt32HostToBig(code); 
    memcpy(str, &swapped, sizeof(swapped)); 
    return str; 
} 

static NSString *formatStatusError(OSStatus status) 
{ 
    if (status == noErr) { 
     return [NSString stringWithFormat:@"No error (%d)", status]; 
    } 

    return [NSString stringWithFormat:@"Error \"%s\" (%d)", 
      codeToString(status), 
      status]; 
} 

static void assertStatusSuccess(OSStatus status) 
{ 
    if (status != noErr) 
    { 
     logMessage(@"Got error %u: '%s'\n", status, codeToString(status)); 
     abort(); 
    } 

} 

static inline AudioObjectPropertyAddress makeGlobalPropertyAddress(AudioObjectPropertySelector selector) 
{ 
    AudioObjectPropertyAddress address = { 
     selector, 
     kAudioObjectPropertyScopeGlobal, 
     kAudioObjectPropertyElementMaster, 

    }; 
    return address; 
} 

static NSString *getStringProperty(AudioDeviceID deviceID, 
            AudioObjectPropertySelector selector) 
{ 
    AudioObjectPropertyAddress address = makeGlobalPropertyAddress(selector); 
    CFStringRef prop; 
    UInt32 propSize = sizeof(prop); 
    OSStatus status = AudioObjectGetPropertyData(deviceID, 
               &address, 
               0, 
               NULL, 
               &propSize, 
               &prop); 
    if (status != noErr) { 
     return formatStatusError(status); 
    } 
    return (__bridge_transfer NSString *)prop; 
} 

static NSString *getURLProperty(AudioDeviceID deviceID, 
           AudioObjectPropertySelector selector) 
{ 
    AudioObjectPropertyAddress address = makeGlobalPropertyAddress(selector); 
    CFURLRef prop; 
    UInt32 propSize = sizeof(prop); 
    OSStatus status = AudioObjectGetPropertyData(deviceID, 
               &address, 
               0, 
               NULL, 
               &propSize, 
               &prop); 
    if (status != noErr) { 
     return formatStatusError(status); 
    } 

    NSURL *url = (__bridge_transfer NSURL *)prop; 
    return url.absoluteString; 
} 

static NSString *getCodeProperty(AudioDeviceID deviceID, 
           AudioObjectPropertySelector selector) 
{ 
    AudioObjectPropertyAddress address = makeGlobalPropertyAddress(selector); 
    UInt32 prop; 
    UInt32 propSize = sizeof(prop); 
    OSStatus status = AudioObjectGetPropertyData(deviceID, 
               &address, 
               0, 
               NULL, 
               &propSize, 
               &prop); 
    if (status != noErr) { 
     return formatStatusError(status); 
    } 

    return [NSString stringWithFormat:@"%s (%d)", 
      codeToString(prop), 
      prop]; 
} 


static NSUInteger getChannelCount(AudioDeviceID deviceID, 
            AudioObjectPropertyScope scope) 
{ 
    AudioObjectPropertyAddress address = { 
     kAudioDevicePropertyStreamConfiguration, 
     scope, 
     kAudioObjectPropertyElementMaster, 
    }; 

    AudioBufferList streamConfiguration; 
    UInt32 propSize = sizeof(streamConfiguration); 
    OSStatus status = AudioObjectGetPropertyData(deviceID, 
               &address, 
               0, 
               NULL, 
               &propSize, 
               &streamConfiguration); 
    assertStatusSuccess(status); 

    NSUInteger channelCount = 0; 
    for (NSUInteger i = 0; i < streamConfiguration.mNumberBuffers; i++) 
    { 
     channelCount += streamConfiguration.mBuffers[i].mNumberChannels; 
    } 

    return channelCount; 
} 

static NSString *getSourceName(AudioDeviceID deviceID, 
           AudioObjectPropertyScope scope) 
{ 
    AudioObjectPropertyAddress address = { 
     kAudioDevicePropertyDataSource, 
     scope, 
     kAudioObjectPropertyElementMaster, 
    }; 

    UInt32 sourceCode; 
    UInt32 propSize = sizeof(sourceCode); 

    OSStatus status = AudioObjectGetPropertyData(deviceID, 
               &address, 
               0, 
               NULL, 
               &propSize, 
               &sourceCode); 
    if (status != noErr) { 
     return formatStatusError(status); 
    } 

    return [NSString stringWithFormat:@"%s (%d)", 
      codeToString(sourceCode), 
      sourceCode]; 
} 

static void inspectDevice(AudioDeviceID deviceID) 
{ 
    logMessage(@"Device %d", deviceID); 
    logMessage(@" - UID:    %@", getStringProperty(deviceID, kAudioDevicePropertyDeviceUID)); 
    logMessage(@" - Model UID:  %@", getStringProperty(deviceID, kAudioDevicePropertyModelUID)); 
    logMessage(@" - Name:   %@", getStringProperty(deviceID, kAudioDevicePropertyDeviceNameCFString)); 
    logMessage(@" - Manufacturer: %@", getStringProperty(deviceID, kAudioDevicePropertyDeviceManufacturerCFString)); 
    logMessage(@" - Input channels: %@", @(getChannelCount(deviceID, kAudioObjectPropertyScopeInput))); 
    logMessage(@" - Output channels: %@", @(getChannelCount(deviceID, kAudioObjectPropertyScopeOutput))); 
    logMessage(@" - Input source: %@", getSourceName(deviceID, kAudioObjectPropertyScopeInput)); 
    logMessage(@" - Output source: %@", getSourceName(deviceID, kAudioObjectPropertyScopeOutput)); 
    logMessage(@" - Transport type: %@", getCodeProperty(deviceID, kAudioDevicePropertyTransportType)); 
    logMessage(@" - Icon:   %@", getURLProperty(deviceID, kAudioDevicePropertyIcon)); 
} 

static void inspectDeviceForSelector(AudioObjectPropertySelector selector) 
{ 
    AudioDeviceID deviceID; 
    UInt32 propSize = sizeof(AudioDeviceID); 
    AudioObjectPropertyAddress address = makeGlobalPropertyAddress(selector); 
    OSStatus status = AudioObjectGetPropertyData(kAudioObjectSystemObject, 
               &address, 
               0, 
               NULL, 
               &propSize, 
               &deviceID); 
    assertStatusSuccess(status); 
    inspectDevice(deviceID); 
} 

static void inspectAllDevices() 
{ 
    // Check the number of devices. 
    AudioObjectPropertyAddress address = makeGlobalPropertyAddress(kAudioHardwarePropertyDevices); 
    UInt32 devicesDataSize; 
    OSStatus status = AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, 
                &address, 
                0, 
                NULL, 
                &devicesDataSize); 
    assertStatusSuccess(status); 

    // Get the devices. 
    int count = devicesDataSize/sizeof(AudioDeviceID); 
    AudioDeviceID deviceIDs[count]; 
    status = AudioObjectGetPropertyData(kAudioObjectSystemObject, 
             &address, 
             0, 
             NULL, 
             &devicesDataSize, 
             deviceIDs); 
    assertStatusSuccess(status); 

    // Inspect them. 
    for (UInt32 i = 0; i < count; i++) { 
     inspectDevice(deviceIDs[i]); 
    } 
} 

int main(int argc, const char * argv[]) 
{ 
    @autoreleasepool { 
     logMessage(@"==== ALL DEVICES ===="); 
     inspectAllDevices(); 
     logMessage(@""); 

     logMessage(@"==== DEFAULT INPUT DEVICE ===="); 
     inspectDeviceForSelector(kAudioHardwarePropertyDefaultInputDevice); 
     logMessage(@""); 

     logMessage(@"==== DEFAULT OUTPUT DEVICE ===="); 
     inspectDeviceForSelector(kAudioHardwarePropertyDefaultOutputDevice); 
     logMessage(@""); 
    } 

    return 0; 
} 
:

이 나는 ​​경우 누구의 장치를 검사하기 위해 사용하고 전체 프로그램은 몇 가지 예를 필요