Is it possible to get status or event when Device Microphone is using by application?

佳昌 邱 21 Reputation points
2020-12-30T07:13:41.997+00:00

Is it possible to get status or event when Device Microphone is using by application?
Recently I need to detect Microphone use status, eg, application use microphone to record a voice and I need to detect or get event from windows 10 to control something. so is any Microsoft API or example code can be refer to.

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,423 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rita Han - MSFT 2,161 Reputation points
    2020-12-30T08:03:13.347+00:00

    Hello @佳昌 邱 ,

    First, you can use IMMDeviceEnumerator to get the audio endpoint associated with the microphone device, it is as a capture audio device when recording a voice. (An audio endpoint is a hardware device that captures or consumes audio data. )

    IMMDeviceCollection* dCol = NULL;
    hr = pEnumerator->EnumAudioEndpoints(eCapture, DEVICE_STATE_ACTIVE, &dCol);

    Then you can get PKEY_Device_FriendlyName to determine whether it is the microphone device you are focusing on.

    Finally, you can emulate active sessions are using the microphone device. And you can use RegisterAudioSessionNotification method registers the client to receive notifications of session events, including changes in the stream state.

    More references: "Managing the Audio Session".

    The following is an example for how to achieve the goal you can refer to:

    BOOL IsMicrophoneRecording()  
    {  
     // #1 Get the audio endpoint associated with the microphone device  
     HRESULT hr = S_OK;  
     IMMDeviceEnumerator* pEnumerator = NULL;  
     IAudioSessionManager2* pSessionManager = NULL;  
     BOOL result = FALSE;  
      
     CoInitialize(0);  
      
     // Create the device enumerator.  
     hr = CoCreateInstance(  
     __uuidof(MMDeviceEnumerator),  
     NULL, CLSCTX_ALL,  
     __uuidof(IMMDeviceEnumerator),  
     (void**)& pEnumerator);  
      
     IMMDeviceCollection* dCol = NULL;  
     hr = pEnumerator->EnumAudioEndpoints(eCapture, DEVICE_STATE_ACTIVE, &dCol);  
     UINT dCount;  
     hr = dCol->GetCount(&dCount);  
     for (UINT i = 0; i < dCount; i++)  
     {  
     IMMDevice* pCaptureDevice = NULL;  
     hr = dCol->Item(i, &pCaptureDevice);  
      
     IPropertyStore* pProps = NULL;  
     hr = pCaptureDevice->OpenPropertyStore(  
     STGM_READ, &pProps);  
      
     PROPVARIANT varName;  
     // Initialize container for property value.  
     PropVariantInit(&varName);  
      
     // Get the endpoint's friendly-name property.  
     hr = pProps->GetValue(  
     PKEY_Device_FriendlyName, &varName);  
      
     std::wstring nameStr(varName.pwszVal);  
      
     // #2 Determine whether it is the microphone device you are focusing on  
     std::size_t found = nameStr.find(L"Microphone");  
     if (found != std::string::npos)  
     {  
     // Print endpoint friendly name.  
     printf("Endpoint friendly name: \"%S\"\n", varName.pwszVal);  
      
     // Get the session manager.  
     hr = pCaptureDevice->Activate(  
     __uuidof(IAudioSessionManager2), CLSCTX_ALL,  
     NULL, (void**)& pSessionManager);  
     break;  
     }  
     }  
      
     // Get session state  
     if (!pSessionManager)  
     {  
     return (result = FALSE);  
     }  
      
     int cbSessionCount = 0;  
     LPWSTR pswSession = NULL;  
      
     IAudioSessionEnumerator* pSessionList = NULL;  
     IAudioSessionControl* pSessionControl = NULL;  
     IAudioSessionControl2* pSessionControl2 = NULL;  
      
     // Get the current list of sessions.  
     hr = pSessionManager->GetSessionEnumerator(&pSessionList);  
      
     // Get the session count.  
     hr = pSessionList->GetCount(&cbSessionCount);  
     //wprintf_s(L"Session count: %d\n", cbSessionCount);  
      
     for (int index = 0; index < cbSessionCount; index++)  
     {  
     CoTaskMemFree(pswSession);  
     SAFE_RELEASE(pSessionControl);  
      
     // Get the <n>th session.  
     hr = pSessionList->GetSession(index, &pSessionControl);  
      
     hr = pSessionControl->QueryInterface(  
     __uuidof(IAudioSessionControl2), (void**)& pSessionControl2);  
      
     // Exclude system sound session  
     hr = pSessionControl2->IsSystemSoundsSession();  
     if (S_OK == hr)  
     {  
     //wprintf_s(L" this is a system sound.\n");  
     continue;  
     }  
      
     // Optional. Determine which application is using Microphone for recording  
     LPWSTR instId = NULL;  
     hr = pSessionControl2->GetSessionInstanceIdentifier(&instId);  
     if (S_OK == hr)  
     {  
     wprintf_s(L"SessionInstanceIdentifier: %s\n", instId);  
     }  
      
     AudioSessionState state;  
     hr = pSessionControl->GetState(&state);  
     switch (state)  
     {  
     case AudioSessionStateInactive:  
     wprintf_s(L"Session state: Inactive\n", state);  
     break;  
     case AudioSessionStateActive:  
     // #3 Active state indicates it is recording, otherwise is not recording.  
     wprintf_s(L"Session state: Active\n", state);  
     result = TRUE;  
     break;  
     case AudioSessionStateExpired:  
     wprintf_s(L"Session state: Expired\n", state);  
     break;  
     }  
     }  
      
    done:  
     // Clean up.  
     SAFE_RELEASE(pSessionControl);  
     SAFE_RELEASE(pSessionList);  
     SAFE_RELEASE(pSessionControl2);  
     SAFE_RELEASE(pSessionManager);  
     SAFE_RELEASE(pEnumerator);  
      
     return result;  
    }  
    

    Result based on above sample code:

    52272-screenshot-2020-12-30-170333.jpg

    Thank you!


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful