VC++ detects headset plugging function (with source code)

VC++ commonly used functions package code a series of articles and source code (advertisement, haha, look at the series and then decide whether to subscribe)

https://blog.csdn.net/kaizi318/article/details/108846326

The source code demo has been uploaded to the Baidu network disk: permanent effective, the code realizes the dynamic detection of earphone insertion and removal functions.

The easiest way is to detect the device notification message, but this is too inaccurate. Although it can be detected, there are too many USB or other messages.

Or plug in the headset, plug in the USB flash drive, USB keyboard, USB mouse, when you plug in and out of these things, it is difficult to distinguish the headset message.

So adding the demo source code in this article can meet your requirements, because I have already mined the pit, and it has been applied to the actual project!

So what to say, come directly to the code:

First need to enumerate the device

1.EnumAudioEndpoints

2. The most important thing is to implement this interface! IMMNotificationClient

3. At this time it is time to monitor the message OnPropertyValueChanged

4. Check whether the device is the headset you want

 

#pragma comment(lib, "winmm.lib")
#include <Mmsystem.h>

#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#include <mmdeviceapi.h>

Guess you like

Origin blog.csdn.net/kaizi318/article/details/108891506