非常感谢您的所有帮助!
对于Windows Vista及更高版本,您不能再使用媒体控制接口,Microsoft有一个新的Core Audio API,您必须访问它来与这些更新的操作系统中的音频硬件进行交互。
Ray Molenkamp编写了一个很好的托管包装器,用于与Core Audio API进行交互,网址在这里: Vista Core Audio API主音量控制
由于我需要能够从XP、Vista和Windows 7静音麦克风,所以我写了一个小Windows麦克风静音库,它在较新的操作系统上使用了Ray的库,而在Windows XP和更早版本中则使用了Gustavo Franco的MixerNative库的部分内容。
您可以使用MCI(媒体控制接口)来访问麦克风并在系统范围内更改它们的音量。请查看下面的代码,它应该将所有系统麦克风的音量设置为0。代码是用C编写的;请查看pinvoke以了解如何将此代码翻译成C#。
#include "mmsystem.h"
...
void MuteAllMics()
{
HMIXER hmx;
mixerOpen(&hmx, 0, 0, 0, 0);
// Get the line info for the wave in destination line
MIXERLINE mxl;
mxl.cbStruct = sizeof(mxl);
mxl.dwComponentType = MIXERLINE_COMPONENTTYPE_DST_WAVEIN;
mixerGetLineInfo((HMIXEROBJ)hmx, &mxl, MIXER_GETLINEINFOF_COMPONENTTYPE);
// find the microphone source line connected to this wave in destination
DWORD cConnections = mxl.cConnections;
for (DWORD j=0; j<cConnections; j++)
{
mxl.dwSource = j;
mixerGetLineInfo((HMIXEROBJ)hmx, &mxl, MIXER_GETLINEINFOF_SOURCE);
if (MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE == mxl.dwComponentType)
{
// Find a volume control, if any, of the microphone line
LPMIXERCONTROL pmxctrl = (LPMIXERCONTROL)malloc(sizeof MIXERCONTROL);
MIXERLINECONTROLS mxlctrl =
{
sizeof mxlctrl,
mxl.dwLineID,
MIXERCONTROL_CONTROLTYPE_VOLUME,
1,
sizeof MIXERCONTROL,
pmxctrl
};
if (!mixerGetLineControls((HMIXEROBJ) hmx, &mxlctrl, MIXER_GETLINECONTROLSF_ONEBYTYPE))
{
DWORD cChannels = mxl.cChannels;
if (MIXERCONTROL_CONTROLF_UNIFORM & pmxctrl->fdwControl)
cChannels = 1;
LPMIXERCONTROLDETAILS_UNSIGNED pUnsigned = (LPMIXERCONTROLDETAILS_UNSIGNED)
malloc(cChannels * sizeof MIXERCONTROLDETAILS_UNSIGNED);
MIXERCONTROLDETAILS mxcd =
{
sizeof(mxcd),
pmxctrl->dwControlID,
cChannels,
(HWND)0,
sizeof MIXERCONTROLDETAILS_UNSIGNED,
(LPVOID) pUnsigned
};
mixerGetControlDetails((HMIXEROBJ)hmx, &mxcd, MIXER_SETCONTROLDETAILSF_VALUE);
// Set the volume to the middle (for both channels as needed)
//pUnsigned[0].dwValue = pUnsigned[cChannels - 1].dwValue = (pmxctrl->Bounds.dwMinimum+pmxctrl->Bounds.dwMaximum)/2;
// Mute
pUnsigned[0].dwValue = pUnsigned[cChannels - 1].dwValue = 0;
mixerSetControlDetails((HMIXEROBJ)hmx, &mxcd, MIXER_SETCONTROLDETAILSF_VALUE);
free(pmxctrl);
free(pUnsigned);
}
else
{
free(pmxctrl);
}
}
}
mixerClose(hmx);
}
在这里你可以找到更多关于此主题的代码
希望这有所帮助,致意
您可以下载整个应用程序的源代码,其中包括静音麦克风、将其选择为录音设备等功能。
http://www.codeguru.com/csharp/csharp/cs_graphics/sound/article.php/c10931/
在Win7中,我有几个麦克风,并且 WindowsMicrophoneMuteLibrary.CoreAudioMicMute 类在这种情况下不正确。
因此,我更改了代码并且现在它很好地工作,因为现在他关闭了所有麦克风的声音,而不仅仅是最后一个被Win7识别的麦克风。
我附上了新的类以放置在适当位置。
http://www.developpez.net/forums/d1145354/dotnet/langages/csharp/couper-micro-sous-win7/