windows7下检测耳机麦克拔插(转)

Posted happykoukou

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了windows7下检测耳机麦克拔插(转)相关的知识,希望对你有一定的参考价值。

原文转自 https://blog.csdn.net/rankun1/article/details/50972990

 

#include "stdafx.h"


#define SAFE_RELEASE(punk)  if ((punk) != NULL)  { (punk)->Release(); (punk) = NULL; }

#include <mmdeviceapi.h>  
#include "iostream"  
using namespace std;


class CMMNotificationClient : public IMMNotificationClient
{
public:
    IMMDeviceEnumerator *m_pEnumerator;
    CMMNotificationClient() :
        _cRef(1),
        m_pEnumerator(NULL)
    {
        //初始化COM  
        ::CoInitialize(NULL);
        HRESULT hr = S_OK;

        //创建接口  
        hr = CoCreateInstance(
            __uuidof(MMDeviceEnumerator), NULL,
            CLSCTX_ALL, __uuidof(IMMDeviceEnumerator),
            (void**)&m_pEnumerator);

        if (hr == S_OK)
        {
            cout << "接口创建成功" << endl;
        }
        else
        {
            cout << "接口创建失败" << endl;
        }
        //注册事件  
        hr = m_pEnumerator->RegisterEndpointNotificationCallback((IMMNotificationClient*)this);
        if (hr == S_OK)
        {
            cout << "注册成功" << endl;
        }
        else
        {
            cout << "注册失败" << endl;
        }
    }

    ~CMMNotificationClient()
    {
        SAFE_RELEASE(m_pEnumerator)
            ::CoUninitialize();
    }


    // IUnknown methods -- AddRef, Release, and QueryInterface   
private:
    LONG _cRef;
    ULONG STDMETHODCALLTYPE AddRef()
    {
        return InterlockedIncrement(&_cRef);
    }

    ULONG STDMETHODCALLTYPE Release()
    {
        ULONG ulRef = InterlockedDecrement(&_cRef);
        if (0 == ulRef)
        {
            delete this;
        }
        return ulRef;
    }

    HRESULT STDMETHODCALLTYPE QueryInterface(
        REFIID riid, VOID **ppvInterface)
    {
        if (IID_IUnknown == riid)
        {
            AddRef();
            *ppvInterface = (IUnknown*)this;
        }
        else if (__uuidof(IMMNotificationClient) == riid)
        {
            AddRef();
            *ppvInterface = (IMMNotificationClient*)this;
        }
        else
        {
            *ppvInterface = NULL;
            return E_NOINTERFACE;
        }
        return S_OK;
    }


    HRESULT STDMETHODCALLTYPE OnDefaultDeviceChanged(
        EDataFlow flow, ERole role,
        LPCWSTR pwstrDeviceId)
    {
        //cout<<"OnDefaultDeviceChanged"<<endl;   
        return S_OK;
    }

    HRESULT STDMETHODCALLTYPE OnDeviceAdded(LPCWSTR pwstrDeviceId)
    {
        return S_OK;
    };

    HRESULT STDMETHODCALLTYPE OnDeviceRemoved(LPCWSTR pwstrDeviceId)
    {

        return S_OK;
    }

    HRESULT STDMETHODCALLTYPE OnDeviceStateChanged(
        LPCWSTR pwstrDeviceId,
        DWORD dwNewState)
    {
        switch (dwNewState)
        {
        case DEVICE_STATE_ACTIVE:
            cout << "监测到可用的麦克风。" << endl;
            break;

        case DEVICE_STATE_DISABLED:
            cout << "监测到麦克风不可用。" << endl;
            break;

        case DEVICE_STATE_NOTPRESENT:
            cout << "监测到麦克风未准备好。" << endl;
            break;

        case DEVICE_STATE_UNPLUGGED:
            cout << "监测到麦克风被拔出。" << endl;
            break;

        default:
            break;
        }

        return S_OK;
    }

    HRESULT STDMETHODCALLTYPE OnPropertyValueChanged(
        LPCWSTR pwstrDeviceId,
        const PROPERTYKEY key)
    {

        return S_OK;
    }
};


int main(int argc, TCHAR* argv[], TCHAR* envp[])
{
    CMMNotificationClient mmClient;

    system("pause");
    return 0;
}

 

以上是关于windows7下检测耳机麦克拔插(转)的主要内容,如果未能解决你的问题,请参考以下文章

台式机上如何配置并使用苹果iPhone的耳机麦克风 并且麦克风开启降噪功能

WinCE 系统下移动存贮设备Storage 的拔插检测

Ubuntu Linux下怎样同时对麦克风及系统声音进行录音

笔记本用minidp外接显示器后,耳机的麦克风用不了?

USB Composite 组合设备之耳机+多路CDC

USB Composite 组合设备之耳机+多路CDC