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!
