C# windows 7控制麦克风的播放音量
我试图调整麦克风的播放音量;但不幸的是我不能。我可以使用NAudio库控制麦克风的录音音量,但我想控制播放音量。windows 7上是否有控制麦克风播放音量的方法。对于我来说,C++或C语言的解决方案都是可以接受的。 以下是解决我问题的一些尝试: (这家伙写了一个可以使麦克风录音音量静音的小程序库) (此解决方案只能使麦克风录音音量静音) 这是使用NAudio解决我的问题的另一次令人沮丧的尝试,这也只会使麦克风录音级别静音:C# windows 7控制麦克风的播放音量,c#,c++,windows-7,microphone,naudio,C#,C++,Windows 7,Microphone,Naudio,我试图调整麦克风的播放音量;但不幸的是我不能。我可以使用NAudio库控制麦克风的录音音量,但我想控制播放音量。windows 7上是否有控制麦克风播放音量的方法。对于我来说,C++或C语言的解决方案都是可以接受的。 以下是解决我问题的一些尝试: (这家伙写了一个可以使麦克风录音音量静音的小程序库) (此解决方案只能使麦克风录音音量静音) 这是使用NAudio解决我的问题的另一次令人沮丧的尝试,这也只会使麦克风录音级别静音: MMDeviceEnumerator DevEnum = new MM
MMDeviceEnumerator DevEnum = new MMDeviceEnumerator();
MMDeviceCollection devices = DevEnum.EnumerateAudioEndPoints(DataFlow.Capture, DeviceState.Active);
MMDevice micDevice = null;
for (int i = 0; i < devices.Count; i++)
{
MMDevice deviceAt = devices[i];
Console.WriteLine(deviceAt.FriendlyName);
if (deviceAt.FriendlyName.ToLower().StartsWith("mikrofon"))
{
micDevice = deviceAt;
}
}
micDevice.AudioEndpointVolume.Mute = true;
MMDeviceEnumerator DevEnum=新的MMDeviceEnumerator();
MMDeviceCollection devices=DevEnum.EnumerateAudioEndPoints(DataFlow.Capture,DeviceState.Active);
MMDevice-micDevice=null;
对于(int i=0;i
我也有同样的问题,我通过使用IDeviceTopology界面找到了答案
我使用了web上浮动的WalkTreeBackwardsFromPart示例
首先,您必须获得渲染设备(扬声器)的部件
HRESULT hr=CoInitialize(空);
如果(失败(小时)){
printf(“失败的协同初始化Ex:hr=0x%08x\n”,hr);
返回线;
}
//获取默认渲染端点
IMMDeviceEnumerator*pEnum=NULL;
hr=CoCreateInstance(
__uuidof(MMDeviceEnumerator)、NULL、CLSCTX_ALL、uuidof(IMMDeviceEnumerator),
(void**)和pEnum
);
如果(失败(小时)){
printf(“无法获取设备枚举器:hr=0x%08x\n”,hr);
coninitialize();
返回线;
}
IMMDevice*pDevice=NULL;
IMMDeviceCollection*pDevices=NULL;
hr=pEnum->EnumAudioEndpoints(eRender、设备状态、活动设备和pDevices);
单位计数;
hr=pDevices->GetCount(&count);
//每个循环打印端点设备的名称。
对于(ULONG i=0;iGetDefaultAudioEndpoint(eRender、eConsole和pDevice);
hr=pDevice->Item(i和pDevice);
如果(失败(小时)){
printf(“无法获取默认渲染设备:hr=0x%08x\n”,hr);
pEnum->Release();
coninitialize();
返回线;
}
pEnum->Release();
//获取该端点的设备拓扑对象
IDeviceTopology*pDT=NULL;
hr=pDevice->Activate(uuu uuidof(IDeviceTopology)、CLSCTX_uall、NULL、(void**)和pDT);
如果(失败(小时)){
printf(“无法获取设备拓扑对象:hr=0x%08x\n”,hr);
pDevice->Release();
coninitialize();
返回线;
}
pDevice->Release();
//获取该端点的单个连接器
IConnector*PConnedPoint=NULL;
hr=pDT->GetConnector(0,&pConnEndpoint);
如果(失败(小时)){
printf(“无法获取端点上的连接器:hr=0x%08x\n”,hr);
pDT->Release();
coninitialize();
返回线;
}
pDT->Release();
//获取正在安装的设备上的连接器
//连接到
//端点上的连接器
i连接器*pConnDevice=NULL;
hr=pconnedpoint->GetConnectedTo(&pConnDevice);
如果(失败(小时)){
printf(“无法获取设备上的连接器:hr=0x%08x\n”,hr);
pConnEndpoint->Release();
coninitialize();
返回线;
}
pConnEndpoint->Release();
//用于IPart的设备连接器上的QI
IPart*pPart=NULL;
hr=pConnDevice->QueryInterface(uu uuidof(IPart),(void**)和pPart);
如果(失败(小时)){
printf(“无法获取部件:hr=0x%08x\n”,hr);
pConnDevice->Release();
coninitialize();
返回线;
}
pConnDevice->Release();
//所有的实际工作都是在这个函数中完成的
hr=从部件(pPart)返回的Walktreebackwards;
如果(失败(小时)){
printf(“无法遍历树:hr=0x%08x\n”,hr);
pPart->Release();
coninitialize();
返回线;
}
pPart->Release();
}
最后一部分叫做WalkTreebackwards,这是魔法发生的地方
我的测试代码在这里所做的只是检查传入的部分,找到标记为“Front Pink In”(这是麦克风的插孔)的部分,当音量显示时,它会设置该部分的音量。这不是最好的代码,但我只是想找到正确的部分
HRESULT WalkTreeBackwardsFromPart(IPart *pPart, int iTabLevel /* = 0 */) {
HRESULT hr = S_OK;
Tab(iTabLevel);
LPWSTR pwszPartName = NULL;
hr = pPart->GetName(&pwszPartName);
if (FAILED(hr)) {
printf("Could not get part name: hr = 0x%08x", hr);
return hr;
}
printf("Part name: %ws\n", *pwszPartName ? pwszPartName : L"(Unnamed)");
//CoTaskMemFree(pwszPartName);
// see if this is a volume node part
IAudioVolumeLevel *pVolume = NULL;
hr = pPart->Activate(CLSCTX_ALL, __uuidof(IAudioVolumeLevel), (void**)&pVolume);
if (E_NOINTERFACE == hr) {
// not a volume node
} else if (FAILED(hr)) {
printf("Unexpected failure trying to activate IAudioVolumeLevel: hr = 0x%08x\n", hr);
return hr;
} else {
// it's a volume node...
hr = DisplayVolume(pVolume, iTabLevel, wcscmp(L"Front Pink In", pwszPartName) == 0);
if (FAILED(hr)) {
printf("DisplayVolume failed: hr = 0x%08x", hr);
pVolume->Release();
return hr;
}
if (wcscmp(L"Microphone Boost", pwszPartName) == 0)
{
SetVolume(pVolume);
}
pVolume->Release();
}
CoTaskMemFree(pwszPartName);
// see if this is a mute node part
IAudioMute *pMute = NULL;
hr = pPart->Activate(CLSCTX_ALL, __uuidof(IAudioMute), (void**)&pMute);
if (E_NOINTERFACE == hr) {
// not a mute node
} else if (FAILED(hr)) {
printf("Unexpected failure trying to activate IAudioMute: hr = 0x%08x\n", hr);
return hr;
} else {
// it's a mute node...
hr = DisplayMute(pMute, iTabLevel);
if (FAILED(hr)) {
printf("DisplayMute failed: hr = 0x%08x", hr);
pMute->Release();
return hr;
}
pMute->Release();
}
// get the list of incoming parts
IPartsList *pIncomingParts = NULL;
//get the list of incoming parts
//hr = pPart->EnumPartsOutgoing(&pIncomingParts);
hr = pPart->EnumPartsIncoming(&pIncomingParts);
if (E_NOTFOUND == hr) {
// not an error... we've just reached the end of the path
Tab(iTabLevel);
printf("No incoming parts at this part\n");
return S_OK;
}
else
{
if (FAILED(hr)) {
printf("Couldn't enum incoming parts: hr = 0x%08x\n", hr);
return hr;
}
UINT nParts = 0;
hr = pIncomingParts->GetCount(&nParts);
if (FAILED(hr)) {
printf("Couldn't get count of incoming parts: hr = 0x%08x\n", hr);
pIncomingParts->Release();
return hr;
}
// walk the tree on each incoming part recursively
for (UINT n = 0; n < nParts; n++) {
IPart *pIncomingPart = NULL;
hr = pIncomingParts->GetPart(n, &pIncomingPart);
if (FAILED(hr)) {
printf("Couldn't get part #%u (0-based) of %u (1-basedSmile hr = 0x%08x\n", n, nParts, hr);
pIncomingParts->Release();
return hr;
}
hr = WalkTreeBackwardsFromPart(pIncomingPart, iTabLevel + 1);
if (FAILED(hr)) {
printf("Couldn't walk tree on part #%u (0-based) of %u (1-basedSmile hr = 0x%08x\n", n, nParts, hr);
pIncomingPart->Release();
pIncomingParts->Release();
return hr;
}
pIncomingPart->Release();
}
pIncomingParts->Release();
}
return S_OK;
}
HRESULT SetVolume(IAudioVolumeLevel *pVolume)
{
HRESULT hr = S_OK;
UINT nChannels = 0;
hr = pVolume->SetLevel(0,20.0,NULL);
return hr;
}
HRESULT DisplayVolume(IAudioVolumeLevel *pVolume, int iTabLevel, bool setVolume) {
HRESULT hr = S_OK;
UINT nChannels = 0;
hr = pVolume->GetChannelCount(&nChannels);
if (FAILED(hr)) {
printf("GetChannelCount failed: hr = %08x\n", hr);
return hr;
}
for (UINT n = 0; n < nChannels; n++) {
float fMinLevelDB, fMaxLevelDB, fStepping, fLevelDB;
hr = pVolume->GetLevelRange(n, &fMinLevelDB, &fMaxLevelDB, &fStepping);
if (FAILED(hr)) {
printf("GetLevelRange failed: hr = 0x%08x\n", hr);
return hr;
}
hr = pVolume->GetLevel(n, &fLevelDB);
if (FAILED(hr)) {
printf("GetLevel failed: hr = 0x%08x\n", hr);
return hr;
}
float levetToSet = fMinLevelDB + ((fMaxLevelDB-fMinLevelDB) * 50 /100.0);
if (setVolume && nChannels == 2)
{
if (n == 0)
{
hr = pVolume->SetLevel(n, fMinLevelDB,NULL);
}
else
{
hr = pVolume->SetLevel(n, fMaxLevelDB,NULL);
}
}
Tab(iTabLevel);
printf(
"Channel %u volume is %.3f dB (range is %.3f dB to %.3f dB in increments of %.3f dB)\n",
n, fLevelDB, fMinLevelDB, fMaxLevelDB, fStepping
);
}
return S_OK;
}
HRESULT DisplayMute(IAudioMute *pMute, int iTabLevel) {
HRESULT hr = S_OK;
BOOL bMuted = FALSE;
hr = pMute->GetMute(&bMuted);
if (FAILED(hr)) {
printf("GetMute failed: hr = 0x%08x\n", hr);
return hr;
}
Tab(iTabLevel);
printf("Mute node: %s\n", bMuted ? "MUTED" : "NOT MUTED");
return S_OK;
}
void Tab(int iTabLevel) {
if (0 >= iTabLevel) { return; }
printf("\t");
Tab(iTabLevel - 1);
}
HRESULT WalkTreeBackwardsFromPart(IPart*pPart,int-iTabLevel/*=0*/){
HRESULT hr=S_正常;
选项卡(iTabLevel);
LPWSTR pwszPartName=NULL;
hr=pPart->GetName(&pwszPartName);
如果(失败(小时)){
printf(“无法获取部件名称:hr=0x%08x”,hr);
返回人力资源;
}
printf(“部件名称:%ws\n”,*pwszPartName?pwszPartName:L”(未命名)”;
//CoTaskMemFree(pwszPartName);
//查看这是否是卷节点部分
IAudioVolumeLevel*pVolume=NULL;
hr=pPart->Activate(CLSCTX\u ALL、\u uuidof(IAudioVolumeLevel)、(void**)和pVolume);
if(E_NOINTERFACE==hr){
//不是卷节点
}否则,如果(失败(hr)){
printf(“尝试激活IAudioVolumeLevel:hr=0x%08x\n”,hr时出现意外故障);
返回人力资源;
}否则{
//这是一个卷节点。。。
hr=显示音量(pVolume、iTabLevel、wcscmp(L“前粉红色输入”,pwszPartName)==0);
如果(失败(小时)){
printf(“显示卷失败:hr=0x%08x”,hr);
pVolume->Release();
返回人力资源;
}
如果(wcscmp(L“话筒升压”,pwszPartName)==0)
{
设定体积(pVolume);
}
pVolume->Release();
}
CoTaskMemFree(pwszPartName);
//查看这是否为静音节点零件
IAudioMute*pMute=NULL;
hr=pPart->Activate(CLSCTX\u ALL、\u uuidof(IAudioMute)、(void**)和pMute);
if(E_NOINTERFACE==hr){
//不是静音节点
}否则,如果(失败(hr)){
普林
HRESULT WalkTreeBackwardsFromPart(IPart *pPart, int iTabLevel /* = 0 */) {
HRESULT hr = S_OK;
Tab(iTabLevel);
LPWSTR pwszPartName = NULL;
hr = pPart->GetName(&pwszPartName);
if (FAILED(hr)) {
printf("Could not get part name: hr = 0x%08x", hr);
return hr;
}
printf("Part name: %ws\n", *pwszPartName ? pwszPartName : L"(Unnamed)");
//CoTaskMemFree(pwszPartName);
// see if this is a volume node part
IAudioVolumeLevel *pVolume = NULL;
hr = pPart->Activate(CLSCTX_ALL, __uuidof(IAudioVolumeLevel), (void**)&pVolume);
if (E_NOINTERFACE == hr) {
// not a volume node
} else if (FAILED(hr)) {
printf("Unexpected failure trying to activate IAudioVolumeLevel: hr = 0x%08x\n", hr);
return hr;
} else {
// it's a volume node...
hr = DisplayVolume(pVolume, iTabLevel, wcscmp(L"Front Pink In", pwszPartName) == 0);
if (FAILED(hr)) {
printf("DisplayVolume failed: hr = 0x%08x", hr);
pVolume->Release();
return hr;
}
if (wcscmp(L"Microphone Boost", pwszPartName) == 0)
{
SetVolume(pVolume);
}
pVolume->Release();
}
CoTaskMemFree(pwszPartName);
// see if this is a mute node part
IAudioMute *pMute = NULL;
hr = pPart->Activate(CLSCTX_ALL, __uuidof(IAudioMute), (void**)&pMute);
if (E_NOINTERFACE == hr) {
// not a mute node
} else if (FAILED(hr)) {
printf("Unexpected failure trying to activate IAudioMute: hr = 0x%08x\n", hr);
return hr;
} else {
// it's a mute node...
hr = DisplayMute(pMute, iTabLevel);
if (FAILED(hr)) {
printf("DisplayMute failed: hr = 0x%08x", hr);
pMute->Release();
return hr;
}
pMute->Release();
}
// get the list of incoming parts
IPartsList *pIncomingParts = NULL;
//get the list of incoming parts
//hr = pPart->EnumPartsOutgoing(&pIncomingParts);
hr = pPart->EnumPartsIncoming(&pIncomingParts);
if (E_NOTFOUND == hr) {
// not an error... we've just reached the end of the path
Tab(iTabLevel);
printf("No incoming parts at this part\n");
return S_OK;
}
else
{
if (FAILED(hr)) {
printf("Couldn't enum incoming parts: hr = 0x%08x\n", hr);
return hr;
}
UINT nParts = 0;
hr = pIncomingParts->GetCount(&nParts);
if (FAILED(hr)) {
printf("Couldn't get count of incoming parts: hr = 0x%08x\n", hr);
pIncomingParts->Release();
return hr;
}
// walk the tree on each incoming part recursively
for (UINT n = 0; n < nParts; n++) {
IPart *pIncomingPart = NULL;
hr = pIncomingParts->GetPart(n, &pIncomingPart);
if (FAILED(hr)) {
printf("Couldn't get part #%u (0-based) of %u (1-basedSmile hr = 0x%08x\n", n, nParts, hr);
pIncomingParts->Release();
return hr;
}
hr = WalkTreeBackwardsFromPart(pIncomingPart, iTabLevel + 1);
if (FAILED(hr)) {
printf("Couldn't walk tree on part #%u (0-based) of %u (1-basedSmile hr = 0x%08x\n", n, nParts, hr);
pIncomingPart->Release();
pIncomingParts->Release();
return hr;
}
pIncomingPart->Release();
}
pIncomingParts->Release();
}
return S_OK;
}
HRESULT SetVolume(IAudioVolumeLevel *pVolume)
{
HRESULT hr = S_OK;
UINT nChannels = 0;
hr = pVolume->SetLevel(0,20.0,NULL);
return hr;
}
HRESULT DisplayVolume(IAudioVolumeLevel *pVolume, int iTabLevel, bool setVolume) {
HRESULT hr = S_OK;
UINT nChannels = 0;
hr = pVolume->GetChannelCount(&nChannels);
if (FAILED(hr)) {
printf("GetChannelCount failed: hr = %08x\n", hr);
return hr;
}
for (UINT n = 0; n < nChannels; n++) {
float fMinLevelDB, fMaxLevelDB, fStepping, fLevelDB;
hr = pVolume->GetLevelRange(n, &fMinLevelDB, &fMaxLevelDB, &fStepping);
if (FAILED(hr)) {
printf("GetLevelRange failed: hr = 0x%08x\n", hr);
return hr;
}
hr = pVolume->GetLevel(n, &fLevelDB);
if (FAILED(hr)) {
printf("GetLevel failed: hr = 0x%08x\n", hr);
return hr;
}
float levetToSet = fMinLevelDB + ((fMaxLevelDB-fMinLevelDB) * 50 /100.0);
if (setVolume && nChannels == 2)
{
if (n == 0)
{
hr = pVolume->SetLevel(n, fMinLevelDB,NULL);
}
else
{
hr = pVolume->SetLevel(n, fMaxLevelDB,NULL);
}
}
Tab(iTabLevel);
printf(
"Channel %u volume is %.3f dB (range is %.3f dB to %.3f dB in increments of %.3f dB)\n",
n, fLevelDB, fMinLevelDB, fMaxLevelDB, fStepping
);
}
return S_OK;
}
HRESULT DisplayMute(IAudioMute *pMute, int iTabLevel) {
HRESULT hr = S_OK;
BOOL bMuted = FALSE;
hr = pMute->GetMute(&bMuted);
if (FAILED(hr)) {
printf("GetMute failed: hr = 0x%08x\n", hr);
return hr;
}
Tab(iTabLevel);
printf("Mute node: %s\n", bMuted ? "MUTED" : "NOT MUTED");
return S_OK;
}
void Tab(int iTabLevel) {
if (0 >= iTabLevel) { return; }
printf("\t");
Tab(iTabLevel - 1);
}