如何将麦克风静音 C#

发布于 2024-08-18 06:43:07 字数 195 浏览 4 评论 0原文

我想知道,如果我想切换麦克风静音/取消静音,编码是什么。我正在制作一个可以在后台运行并拾取按键事件并切换麦克风静音/取消静音的程序。任何有关该编码的帮助都会非常有帮助。我对 C# 还很陌生,这只是我想要制作的一个非常简单的程序。这就是它所做的全部,它会监听空格键的按键,即使程序位于后台,然后当按下空格键时,它会将麦克风静音/取消静音。

感谢您提供的所有帮助!

I wanted to know, what would the coding be if I wanted to toggle mute/unmute of my microphone. I am making a program that can run in the background and pickup a keypress event and toggle mute/unmute of the mic. Any help with any of that coding would be very helpful. I am pretty new to C#, and this is just a really simple program I wanted to make. That is all it does, is it will listen for keypress of the spacebar, even when the program is in the background, then when the spacebar is pressed it will mute/unmute the mic.

Thank you for any and all help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

泪是无色的血 2024-08-25 06:43:07

对于 Windows Vista 及更高版本,您不能再使用媒体控制界面,Microsoft 有一个新的

Ray Molenkamp 在这里编写了一个很好的托管包装器,用于与 Core Audio API 交互:
Vista Core Audio API 主音量控制

因为我需要能够将麦克风静音XP、Vista 和 Windows 7 我写了一些 Windows 麦克风静音库 在较新的操作系统上使用 Ray 的库以及 Gustavo Franco 的 MixerNative 库 适用于 Windows XP 及更早版本。

For Windows Vista and newer, you can no longer use the Media Control Interface, Microsoft has a new Core Audio API that you must access to interface with audio hardware in these newer operating systems.

Ray Molenkamp wrote a nice managed wrapper for interfacing with the Core Audio API here:
Vista Core Audio API Master Volume Control

Since I needed to be able to mute the microphone from XP, Vista and Windows 7 I wrote a little Windows Microphone Mute Library which uses Ray's library on the newer operating systems and parts of Gustavo Franco's MixerNative library for Windows XP and older.

白衬杉格子梦 2024-08-25 06:43:07

您可以下载整个应用程序的源代码,该应用程序具有使麦克风静音、选择其作为录音设备等功能。

http://www.codeguru.com/csharp/csharp/cs_graphics/sound/article.php/c10931/

You can download the source of a whole application which has muting the microphone, selecting it as a recording device, etc.

http://www.codeguru.com/csharp/csharp/cs_graphics/sound/article.php/c10931/

大姐,你呐 2024-08-25 06:43:07

您可以使用 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); 
}

这里可以找到有关此主题的更多代码

希望这有帮助,问候

you can use MCI (Media Control Interface) to access mics and change their volume system wise. Check the code below it should be setting volume to 0 for all system microphones. Code is in c; check pinvoke for details on how to translate this code to 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); 
}

here you can find more code on this topic

hope this helps, regards

撧情箌佬 2024-08-25 06:43:07

我在 win7 中有多个麦克风,并且类 WindowsMicrophoneMuteLibrary.CoreAudioMicMute 在这种情况下不正确。

所以我更改了代码并且效果很好,因为现在他的杯子吹口哨了所有麦克风,而不仅仅是win7最后识别的。

我正在附加新课程以使其到位。

http://www.developpez.net/论坛/d1145354/dotnet/langages/csharp/couper-micro-sous-win7/

I have several microphones in win7 and class WindowsMicrophoneMuteLibrary.CoreAudioMicMute is incorrect in this case.

so I change the code and works great because now his cup Whistle all microphones and not just in the last recognized by win7.

I am attaching the new class to put in place.

http://www.developpez.net/forums/d1145354/dotnet/langages/csharp/couper-micro-sous-win7/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文