开发者

How to mute the microphone 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 i开发者_C百科t 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!


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.


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/


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


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/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜