如何使用 C/C++ 在 Windows 7 中使麦克风静音?
我制作了一个使用 WinAPI 使麦克风静音的程序,它似乎在 Windows XP 中完美运行,但在 Windows 7 中不起作用.是否可以在 Windows 7 中使用 WinAPI 控制麦克风音量或静音?
void setVolume(DWORD volume) {HMIXER 混合器;if (mixerOpen(&mixer, 0, 0, 0, 0) != MMSYSERR_NOERROR) {MessageBoxW(NULL, L"错误:mixerOpen()", NULL, MB_ICONHAND);返回;}//获取线路信息MIXERC??APS 混合盖;MIXERLINE 混合器线;混合器GetDevCaps(0, &mixcaps, sizeof(MIXERC??APS));MixerLine.cbStruct = sizeof(MIXERLINE);MixerLine.dw??ComponentType = MIXERLINE_COMPONENTTYPE_DST_WAVEIN;混合器线.dwSource = 0;MixerLine.dw??Destination = 0;if (mixerGetLineInfo(reinterpret_cast(mixer), &mixerLine, MIXER_GETLINEINFOF_SOURCE)!= MMSYSERR_NOERROR) {MessageBoxW(NULL, L"错误:mixerGetLineInfo()", NULL, MB_ICONHAND);返回;}//获取mixerline的控制权MIXERC??ONTROL 混音器Ctrl;MIXERLINECONTROLSMixerLineCtrl.cbStruct = sizeof(MIXERLINECONTROLS);MixerLineCtrl.dwLineID=mixerLine.dw??LineID;mixLineCtrl.dwControlType = MIXERC??ONTROL_CONTROLTYPE_VOLUME;混合器LineCtrl.cControls = 1;MixerLineCtrl.pamxctrl = &mixerCtrl;MixerLineCtrl.cbmxctrl = sizeof(MIXERC??ONTROL);混合器LineCtrl.cControls = 5;if (mixerGetLineControls(reinterpret_cast(mixer), &mixerLineCtrl, MIXER_GETLINECONTROLSF_ONEBYTYPE)!= MMSYSERR_NOERROR) {MessageBoxW(NULL, L"错误:mixerGetLineControls()", NULL, MB_ICONHAND);返回;}//体积..MIXERC??ONTROLDETAILS MixerCtrlDetails;MIXERC??ONTROLDETAILS_UNSIGNED 混合器CtrlDetailsUnsigned;MixerCtrlDetailsUnsigned.dwValue = 音量;混音器CtrlDetails.dwControlID =混音器Ctrl.dwControlID;MixerCtrlDetails.cbStruct = sizeof(MIXERC??ONTROLDETAILS);混合器CtrlDetails.cMultipleItems = 0;MixerCtrlDetails.paDetails = &mixerCtrlDetailsUnsigned;MixerCtrlDetails.cbDetails = sizeof(MIXERC??ONTROLDETAILS_UNSIGNED);混合器CtrlDetails.cChannels = 1;if (mixerSetControlDetails(reinterpret_cast(mixer), &mixerCtrlDetails, MIXER_OBJECTF_HMIXER | MIXER_GETCONTROLDETAILSF_VALUE)!= MMSYSERR_NOERROR) {MessageBoxW(NULL, L"错误:mixerSetControlDetails()", NULL, MB_ICONHAND);返回;}混合器关闭(混合器);}
解决方案您需要使用
#include stdafx.h"#include <stdio.h>#include <windows.h>#include <mmdeviceapi.h>#include <endpointvolume.h>无效用法(){printf("用法:
");printf("SetVolume [报告当前音量]
");printf("SetVolume -d <new volume in decibels> [将当前默认渲染设备音量设置为新音量]
");printf("SetVolume -f <new volume as an Amplitude scalar> [将当前默认渲染设备音量设置为新音量]
");}int _tmain(int argc, _TCHAR* argv[]){HRESULT 小时;布尔分贝 = 假;布尔标量 = 假;双新卷;if (argc != 3 && argc != 1){用法();返回-1;}如果(argc == 3){if (argv[1][0] == '-'){if (argv[1][1] == 'f'){标量 = 真;}否则 if (argv[1][1] == 'd'){分贝=真;}}别的{用法();返回-1;}新卷 = _tstof(argv[2]);}//-------------------------CoInitialize(NULL);IMMDeviceEnumerator *deviceEnumerator = NULL;hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER, __uuidof(IMMDeviceEnumerator), (LPVOID *)&deviceEnumerator);IMMDevice *defaultDevice = NULL;hr = deviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &defaultDevice);deviceEnumerator->Release();设备枚举器 = NULL;IAudioEndpointVolume *endpointVolume = NULL;hr = defaultDevice->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_INPROC_SERVER, NULL, (LPVOID *)&endpointVolume);defaultDevice->Release();默认设备=空;//-------------------------浮动电流量 = 0;endpointVolume->GetMasterVolumeLevel(¤tVolume);printf("当前音量为:%f
", currentVolume);hr = endpointVolume->GetMasterVolumeLevelScalar(¤tVolume);printf("当前体积作为标量是:%f
", currentVolume);如果(分贝){hr = endpointVolume->SetMasterVolumeLevel((float)newVolume, NULL);}否则如果(标量){hr = endpointVolume->SetMasterVolumeLevelScalar((float)newVolume, NULL);}endpointVolume->Release();联合初始化();返回0;}
I made a program to mute microphone using WinAPI and it seems to work perfectly in Windows XP but doesn't do a thing in Windows 7. Is it possible to control microphone volume or mute with WinAPI in Windows 7?
void setVolume(DWORD volume) {
HMIXER mixer;
if (mixerOpen(&mixer, 0, 0, 0, 0) != MMSYSERR_NOERROR) {
MessageBoxW(NULL, L"Error: mixerOpen()", NULL, MB_ICONHAND);
return;
}
// Get the line info
MIXERCAPS mixcaps;
MIXERLINE mixerLine;
mixerGetDevCaps(0, &mixcaps, sizeof(MIXERCAPS));
mixerLine.cbStruct = sizeof(MIXERLINE);
mixerLine.dwComponentType = MIXERLINE_COMPONENTTYPE_DST_WAVEIN;
mixerLine.dwSource = 0;
mixerLine.dwDestination = 0;
if (mixerGetLineInfo(reinterpret_cast<HMIXEROBJ>(mixer), &mixerLine, MIXER_GETLINEINFOF_SOURCE)
!= MMSYSERR_NOERROR) {
MessageBoxW(NULL, L"Error: mixerGetLineInfo()", NULL, MB_ICONHAND);
return;
}
// Get control for mixerline
MIXERCONTROL mixerCtrl;
MIXERLINECONTROLS mixerLineCtrl;
mixerLineCtrl.cbStruct = sizeof(MIXERLINECONTROLS);
mixerLineCtrl.dwLineID = mixerLine.dwLineID;
mixerLineCtrl.dwControlType = MIXERCONTROL_CONTROLTYPE_VOLUME;
mixerLineCtrl.cControls = 1;
mixerLineCtrl.pamxctrl = &mixerCtrl;
mixerLineCtrl.cbmxctrl = sizeof(MIXERCONTROL);
mixerLineCtrl.cControls = 5;
if (mixerGetLineControls(reinterpret_cast<HMIXEROBJ>(mixer), &mixerLineCtrl, MIXER_GETLINECONTROLSF_ONEBYTYPE)
!= MMSYSERR_NOERROR) {
MessageBoxW(NULL, L"Error: mixerGetLineControls()", NULL, MB_ICONHAND);
return;
}
// Volume..
MIXERCONTROLDETAILS mixerCtrlDetails;
MIXERCONTROLDETAILS_UNSIGNED mixerCtrlDetailsUnsigned;
mixerCtrlDetailsUnsigned.dwValue = volume;
mixerCtrlDetails.dwControlID = mixerCtrl.dwControlID;
mixerCtrlDetails.cbStruct = sizeof(MIXERCONTROLDETAILS);
mixerCtrlDetails.cMultipleItems = 0;
mixerCtrlDetails.paDetails = &mixerCtrlDetailsUnsigned;
mixerCtrlDetails.cbDetails = sizeof(MIXERCONTROLDETAILS_UNSIGNED);
mixerCtrlDetails.cChannels = 1;
if (mixerSetControlDetails(reinterpret_cast<HMIXEROBJ>(mixer), &mixerCtrlDetails, MIXER_OBJECTF_HMIXER | MIXER_GETCONTROLDETAILSF_VALUE)
!= MMSYSERR_NOERROR) {
MessageBoxW(NULL, L"Error: mixerSetControlDetails()", NULL, MB_ICONHAND);
return;
}
mixerClose(mixer);
}
解决方案
You need to use the new audio APIs introduced by Windows Vista.
Here is an example.
*EDIT: Visual Studio 2015 (C++):
Usange:
// To 100%
ConsoleApplication3.exe -f 1
// To 0%
ConsoleApplication3.exe -f 0
// To 50%
ConsoleApplication3.exe -f 0.50
Code:
#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
#include <mmdeviceapi.h>
#include <endpointvolume.h>
void Usage()
{
printf("Usage:
");
printf(" SetVolume [Reports the current volume]
");
printf(" SetVolume -d <new volume in decibels> [Sets the current default render device volume to the new volume]
");
printf(" SetVolume -f <new volume as an amplitude scalar> [Sets the current default render device volume to the new volume]
");
}
int _tmain(int argc, _TCHAR* argv[])
{
HRESULT hr;
bool decibels = false;
bool scalar = false;
double newVolume;
if (argc != 3 && argc != 1)
{
Usage();
return -1;
}
if (argc == 3)
{
if (argv[1][0] == '-')
{
if (argv[1][1] == 'f')
{
scalar = true;
}
else if (argv[1][1] == 'd')
{
decibels = true;
}
}
else
{
Usage();
return -1;
}
newVolume = _tstof(argv[2]);
}
// -------------------------
CoInitialize(NULL);
IMMDeviceEnumerator *deviceEnumerator = NULL;
hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER, __uuidof(IMMDeviceEnumerator), (LPVOID *)&deviceEnumerator);
IMMDevice *defaultDevice = NULL;
hr = deviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &defaultDevice);
deviceEnumerator->Release();
deviceEnumerator = NULL;
IAudioEndpointVolume *endpointVolume = NULL;
hr = defaultDevice->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_INPROC_SERVER, NULL, (LPVOID *)&endpointVolume);
defaultDevice->Release();
defaultDevice = NULL;
// -------------------------
float currentVolume = 0;
endpointVolume->GetMasterVolumeLevel(¤tVolume);
printf("Current volume in dB is: %f
", currentVolume);
hr = endpointVolume->GetMasterVolumeLevelScalar(¤tVolume);
printf("Current volume as a scalar is: %f
", currentVolume);
if (decibels)
{
hr = endpointVolume->SetMasterVolumeLevel((float)newVolume, NULL);
}
else if (scalar)
{
hr = endpointVolume->SetMasterVolumeLevelScalar((float)newVolume, NULL);
}
endpointVolume->Release();
CoUninitialize();
return 0;
}
相关文章