gpt4 book ai didi

objective-c - 输出设备的类型(耳机等)

转载 作者:行者123 更新时间:2023-12-03 16:30:32 25 4
gpt4 key购买 nike

出于用户界面的原因,我想知道输出设备是什么类型的设备。例如,如果音频默认通过耳机传输,我需要显示耳机图标。

为了获取此信息,我将 AudioObjectGetPropertyData()kAudioDevicePropertyDataSource 结合使用。这将返回 'ispk'(表示内置扬声器)、'hdpn'(表示耳机)等。

如果我使用连接耳机的外部 USB-C 集线器,该代码将不起作用。函数调用返回错误 2003332927(即 'who?')。

我能得到的唯一信息是UID是AppleUSBAudioEngine:Burr-Brown from TI:USB audio CODEC:14412000:2,名称是USB audio CODEC制造商是 来自 TI 的 Burr-Brown

你知道我是否可以得到更多有用的信息吗?

这是我的测试代码:

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

最佳答案

我找到了答案。 kAudioDevicePropertyTransportType 是我想要的。

Cmd-单击 Xcode 中的枚举向我显示了错误的文件 AudioHarwareDeprecated.h 而不是正确的 AudioHardwareBase.h,因此我可以看到一些枚举值,但是不是我想要的。

这是我用来检查设备的完整程序,以防有人需要一些示例:

// Compile with: clang -framework Foundation -framework CoreAudio -lobjc
#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;
}

关于objective-c - 输出设备的类型(耳机等),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46075637/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com