|
发表于 2024-9-8 10:11:59
来自手机
|
显示全部楼层
xyn1 发表于 2024-9-7 15:38
坛友好,请教您,想通过电脑3.5话筒接口,在Windoes上开发一个应用,具体就是话筒接口两根线,断开,接通, ...
試試看
#include <windows.h>
#include <mmsystem.h>
#include <iostream>
#pragma comment(lib, "winmm.lib")
// Callback function to handle device change events
void CALLBACK DeviceChangeCallback(
HWAVEIN hwi,
UINT uMsg,
DWORD_PTR dwInstance,
DWORD_PTR dwParam1,
DWORD_PTR dwParam2
) {
if (uMsg == MM_WIM_OPEN) {
// Microphone was plugged in
MessageBox(NULL, "Microphone plugged in", "Notification", MB_OK);
} else if (uMsg == MM_WIM_CLOSE) {
// Microphone was unplugged
MessageBox(NULL, "Microphone unplugged", "Notification", MB_OK);
}
}
// Entry point of the application
int main() {
WAVEINCAPS waveInCaps;
MMRESULT result;
// Check the number of wave input devices
UINT numDevices = waveInGetNumDevs();
if (numDevices == 0) {
MessageBox(NULL, "No wave input devices available.", "Error", MB_OK);
return 1;
}
// Get information about the first device
result = waveInGetDevCaps(0, &waveInCaps, sizeof(WAVEINCAPS));
if (result != MMSYSERR_NOERROR) {
MessageBox(NULL, "Failed to get wave input device capabilities.", "Error", MB_OK);
return 1;
}
// Open the wave input device
HWAVEIN hWaveIn;
result = waveInOpen(&hWaveIn, 0, NULL, (DWORD_PTR)DeviceChangeCallback, 0, CALLBACK_FUNCTION);
if (result != MMSYSERR_NOERROR) {
MessageBox(NULL, "Failed to open wave input device.", "Error", MB_OK);
return 1;
}
// Run a message loop to process events
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
// Close the wave input device
waveInClose(hWaveIn);
return 0;
}
|
|