How to judge whether the audio device is unplugged or occupied

哲敏 陈 86 Reputation points
2022-04-15T09:41:52.567+00:00

I'm dealing with some compatibility scenarios related to audio devices through C++ code in Windows System. For example, when the software is collecting microphone data, if the device is unplugged, or the microphone permission of the application is disabled, or the microphone device is preempted by other software in exclusive mode.

When collecting microphone data, if the above three situations occur, the IAudioCaptureClient::GetBuffer interface returns the same error code [HRESULT = - 2004287484], so I can't tell what the problem is through the error code.

Now I can get whether the application has microphone permission, so I just need to distinguish whether the audio device is unplugged or preempted by other applications in exclusive mode. I tried to traverse the system audio device list again when receiving the error code returned by IAudioCaptureClient::GetBuffer. If there is no device matching the specified ID in the system audio device list, I think it has been pulled out.

However, after trying many times, I found that this method is not reliable. I listened to the IMMNotificationClient::OnDeviceStateChanged callback. When the device is unplugged, I always receive the message of speaker device disconnect first, and then receive the message of microphone device disconnect again after hundreds of milliseconds. When I received the speaker device disconnect message, I went through the system audio device list and found that the unplugged device was still in the list.

So is there a better way to judge whether the device is pulled out or preempted by other applications in exclusive mode?I hope there can be a scheme that can be processed through C++ code.

I look forward to your enthusiastic reply. Thank you!

Windows development | Windows API - Win32
Windows for business | Windows Client for IT Pros | Devices and deployment | Other
Developer technologies | C++
0 comments No comments
{count} votes

Accepted answer
  1. Xiaopo Yang - MSFT 12,731 Reputation points Microsoft External Staff
    2022-04-18T02:52:36.027+00:00

    According to Recovering from an Invalid-Device Error, Perhaps you need the IAudioSessionEvents interface.

    An application can determine more precisely the cause of an invalid-device error by registering to receive a notification when a session loses its connection to a device. To enable this notification, the application implements an IAudioSessionEvents interface and calls the IAudioSessionControl::RegisterAudioSessionNotification method to register the interface. When an invalid-device error causes the session to be disconnected, WASAPI calls the IAudioSessionEvents::OnSessionDisconnected method in the registered interface. Through this method, WASAPI informs the application of the reason for the disconnection. In Windows Vista, the OnSessionDisconnected call identifies the following reasons:

    The user removed the audio endpoint device.
    The Windows audio service has shut down.
    The preferred stream format changed for the device that the audio session is connected to.
    The user logged off the Windows Terminal Services (WTS) session that the audio session was running in. For more information about WTS sessions, see the Windows SDK documentation.
    The WTS session that the audio session was running in was disconnected.
    The (shared-mode) audio session was disconnected to make the audio endpoint device available for an exclusive-mode connection.


2 additional answers

Sort by: Most helpful
  1. Castorix31 90,686 Reputation points
    2022-04-15T15:16:49.583+00:00

    To get notified when the microphone is unplugged, it works fine on my OS (Windows 10 21H1) with RegisterDeviceNotification
    and DEVINTERFACE_AUDIO_CAPTURE for dbcc_classguid
    In WM_DEVICECHANGE, I get DBT_DEVICEREMOVECOMPLETE event with right infos in
    PDEV_BROADCAST_DEVICEINTERFACE pInfos = (PDEV_BROADCAST_DEVICEINTERFACE)lParam;


  2. Edulete Technologies LLP 1 Reputation point
    2022-04-16T14:02:03.49+00:00

    There are three ways to judge whether the audio device is unplugged or occupied:

    • Visual inspection: look at the audio jack and see if there is a plug in it.
      • Audible inspection: If you can hear sound coming from the audio jack, then the audio device is plugged in.
      • Tactile inspection: If you can feel a plug in the audio jack, then the audio device is plugged in.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.