Search code examples
c#microphonemute

Muting Microphone in C#


i am currently trying to write a programm in C# that mutes and unmutes the microphone in order to simulate a stuttering effect. I have tried googling, but all i could find was some entrys that are 10 years old. I tried using MediaCommands and then MicrophoneVolumeMute but couldnt get it to work. Thanks in advance


Solution

  • There are many opensource projects, written in pure C# that do exactly this job. For example, you could check out https://github.com/DanielGilbert/dgMicMute. Unfortunately it's not that easy anymore to globally toggle microphones since Win7. For this reason, the required code is quite substantial. As starting point you can read DgMic.cs.

    If we consider using external libraries, things get much easier. Here's some code that works with NAudio:

    using var enumerator = new NAudio.CoreAudioApi.MMDeviceEnumerator();
    var commDevice = enumerator.GetDefaultAudioEndpoint(DataFlow.Capture, Role.Communications);
    commDevice.AudioEndpointVolume.Mute = true; //or false
    

    To make this work, you'll have to add a reference to the NAudio NuGet package.