views:

378

answers:

2

Hi! 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!

A: 

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/

TGadfly
+1  A: 

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

serge_gubenko