Core Audio and Phantom Device ID

So here is what happens.

I am trying to work with Core Audio, especially input devices. I want to turn off the sound, change the volume, etc. Etc. I came across something completely strange that I cannot understand. So far, Google has not helped.

When I query the system and request a list of all audio devices, I get an array of device identifiers. In this case, 261, 259, 263, 257.

Using kAudioDevicePropertyDeviceName, I get the following:

261: built-in microphone
259: Built-in input
263: built-in output
257: iPhoneSimulatorAudioDevice

This is good and good.

// This method returns an NSArray of all the audio devices on the system, both input and // On my system, it returns 261, 259, 263, 257 - (NSArray*)getAudioDevices { AudioObjectPropertyAddress propertyAddress = { kAudioHardwarePropertyDevices, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster }; UInt32 dataSize = 0; OSStatus status = AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &propertyAddress, 0, NULL, &dataSize); if(kAudioHardwareNoError != status) { MZLog(@"Unable to get number of audio devices. Error: %d",status); return NULL; } UInt32 deviceCount = dataSize / sizeof(AudioDeviceID); AudioDeviceID *audioDevices = malloc(dataSize); status = AudioObjectGetPropertyData(kAudioObjectSystemObject, &propertyAddress, 0, NULL, &dataSize, audioDevices); if(kAudioHardwareNoError != status) { MZLog(@"AudioObjectGetPropertyData failed when getting device IDs. Error: %d",status); free(audioDevices), audioDevices = NULL; return NULL; } NSMutableArray* devices = [NSMutableArray array]; for(UInt32 i = 0; i < deviceCount; i++) { MZLog(@"device found: %d",audioDevices[i]); [devices addObject:[NSNumber numberWithInt:audioDevices[i]]]; } free(audioDevices); return [NSArray arrayWithArray:devices]; } 

The problem occurs when I query the system and request the default input device identifier. This method returns identifier 269, which is not specified in the array of all devices.

If I try to use kAudioDevicePropertyDeviceName to get the device name, an empty string is returned to me. Although it does not have a name, if I disable this device identifier, my built-in microphone will mute. And vice versa, if I turn off the 261 ID, which is called "Built-in microphone", my microphone does not disable.

 // Gets the current default audio input device // On my system, it returns 269, which is NOT LISTED in the array of ALL audio devices - (AudioDeviceID)defaultInputDevice { AudioDeviceID defaultAudioDevice; UInt32 propertySize = 0; OSStatus status = noErr; AudioObjectPropertyAddress propertyAOPA; propertyAOPA.mElement = kAudioObjectPropertyElementMaster; propertyAOPA.mScope = kAudioObjectPropertyScopeGlobal; propertyAOPA.mSelector = kAudioHardwarePropertyDefaultInputDevice; propertySize = sizeof(AudioDeviceID); status = AudioHardwareServiceGetPropertyData(kAudioObjectSystemObject, &propertyAOPA, 0, NULL, &propertySize, &defaultAudioDevice); if(status) { //Error NSLog(@"Error %d retreiving default input device",status); return 0; } return defaultAudioDevice; } 

To make things even more confusing, if I manually switch my input to Line In and restart the program, I get the ID 259 when I request the default input device, in which in the array of all devices.

So, we summarize:

I am trying to interact with input devices on my system. If I try to interact with the device ID 261, which is my โ€œbuilt-in microphone,โ€ nothing happens. If I try to interact with the device identifier 269, which is apparently a phantom identifier, my built-in microphone will be affected. Id 269 is returned when I request a system for the default input device, but it is not specified when I request a system for a list of all devices.

Does anyone know what is going on? Am I just losing my mind?

Thanks in advance!

+7
source share
1 answer

Fixed.

First, the phantom device identifier was just the virtual device that the system used.

Secondly, the reason I couldn't mute or do anything with actual devices was because I used AudioHardwareServiceSetPropertyData instead of AudioObjectSetPropertyData.

Now everything works.

+3
source

All Articles