Detect when (internal or external) microphone is being used

Hello,

I used kAudioDevicePropertyDeviceIsRunningSomewhere to check if an internal or external microphone is being used. My code works well for the internal microphone, and for microphones which are connected using a cable. External microphones which are connected using bluetooth are not reporting their status. The status is always requested successfully, but it is always reported as inactive.

Main relevant parts in my code :

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

  };
  return address;
}

static BOOL getBoolProperty(AudioDeviceID deviceID,
                            AudioObjectPropertySelector selector)
{
  AudioObjectPropertyAddress const address =
      makeGlobalPropertyAddress(selector);
  UInt32 prop;
  UInt32 propSize = sizeof(prop);
  OSStatus const status =
      AudioObjectGetPropertyData(deviceID, &address, 0, NULL, &propSize, &prop);
  if (status != noErr) {
    return 0; //this line never gets executed in my tests. The call above always succeeds, but it always gives back "false" status.
  }

  return static_cast<BOOL>(prop == 1);
}

...
  __block BOOL microphoneActive = NO;
  iterateThroughAllInputDevices(^(AudioObjectID object, BOOL *stop) {
    if (getBoolProperty(object, kAudioDevicePropertyDeviceIsRunningSomewhere) !=
        0) {
      microphoneActive = YES;
      *stop = YES;
    }
  });

What could cause this and how could it be fixed?

Thank you for your help in advance!

  • Are you using Obj-c to fix this issue? Have you found a solution?

  • I got stuck in the same problem. Have you found a solution??

Add a Comment