Detecting if the microphone is on

梦想与她 提交于 2019-12-11 06:31:08

问题


Is there a way to programmatically detect whether the microphone is on on Windows?


回答1:


No, microphones don't tell you whether they're ‘on’ or that a particular sound channel is connected to a microphone device. The best you can do is to read audio data from the input channel you suspect to be a microphone (eg. the Windows default input device/channel), and see if there's any signal on it.

To do that you'd have to remove any DC offset and look for any signal above a reasonable noise floor. (Be generous: many cheap audio input devices are quite noisy even when there is no signal coming in. A mid-band filter/FFT would also be useful to detect only signals in the mid-range of a voice and not low-frequency hum and transient clicks.)




回答2:


This is not tested in any way, but I would try to read some samples and see if there is any variation. If the mike is on then you should get different values from the ambient sounds. If the mike is off you should get a 0. Again this is just how I imagine things should work - I don't know if they actually work that way.




回答3:


Due to a happy accident, I may have discovered that yes there is a way to detect the presence of a connected microphone.

If your windows "recording devices" shows "no microphone", then this approach (using the Microsoft Speech API) will work and confirm you have no mic. If windows however thinks you have a mic, this won't disagree.

#include <sapi.h>
#include <sapiddk.h>
#include <sphelper.h>

CComPtr<ISpRecognizer>  m_cpEngine;
m_cpEngine.CoCreateInstance(CLSID_SpInprocRecognizer);
CComPtr<ISpObjectToken> pAudioToken;
HRESULT hr = SpGetDefaultTokenFromCategoryId(SPCAT_AUDIOIN, &pAudioToken);
if (FAILED(hr))  ::OutputDebugString("no input, aka microphone, detected");

more specifically, hr will return this result:

SPERR_NOT_FOUND 0x8004503a  -2147200966
The requested data item (data key, value, etc.) was not found.


来源:https://stackoverflow.com/questions/1559542/detecting-if-the-microphone-is-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!