English 中文(简体)
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 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!

问题回答

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/





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签