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
Asked
Active
Viewed 875 times
0

señor.woofers
- 119
- 2
- 8
-
1can you share your code? – Baahubali Feb 01 '21 at 00:31
1 Answers
0
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.

Simon
- 1,814
- 20
- 37
-
-
I didn't download vote it, but I'm going to take a guess it has to do with untested code. You have a semicolon on the end of line 1, meaning, the next two lines would fail. You have no brackets around the "var enumerator..." or the execution. This code will not compile. When providing examples, all code should be tested before submission. Hope that helps. – Chizl Apr 14 '23 at 14:54
-
@Chizl This is valid code since C# 8. That language version introduced using statements (without blocks) The documentation at https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/using might be of interest to you. – Simon Apr 14 '23 at 15:07
-
You using block has a semi-colon on the end. Meaning, the object is closed at that time. – Chizl Apr 14 '23 at 15:13
-
I think you're mistaken. Please see this SO answer: https://stackoverflow.com/a/60767571/742404 – Simon Apr 14 '23 at 15:37
-
Correct, see the //disposed right under it? Is there a semi-colon after the "using" line? using requires IDisposal. You can use >> using(var v = object) v.proper = "test"; << However, because you have >> using var v = object; <<<- You can't use that object after that Semi-Colon, ";" . enumerator can not be used in your example after the first line because of the semicolon on the end. – Chizl Apr 14 '23 at 15:51
-
Please see this compiling example https://sharplab.io/#v2:EYLgxg9gTgpgtADwGwBYA0AXEUCuA7AHwAEAmARgFgAoaogBgAIARASwGcAHCNgQ2ABsYDACZkGAXgZ4YAd2bsuvATAAUASgDc1UQDoAksJh4MLAGYBPdVppUiAZgal5nbn0EMQDPaxdLB1AG9qBhDHBx9FN1U1BgCGAF9qYND7RxRnRWjYhOSQ1KJ0gyMTC3UJAD5HMgBOFQAiAAkYfn4IOs1qRKogA – Simon Apr 14 '23 at 15:59
-
Difference is, that's not .NET framework. .NET Framework will not allow that, as you can see for yourself. https://sharplab.io/#v2:EYLgHgbALAPgAgJgIwFgBQ64AYAEARASwGcAHAeyIENgAbAUxwBMkcBeHAOzoHd9jyqtOgAoAlAG50zAHQBJRnQ4AXAgDMAnmMkY0cAMw5EfUhWr0cIQ0jmETg+ugDe6HK8MHbAsyNE5HOAF90Fzd9QyhjAR8/QJDXMLgI+UUVDTE2AD4raTgkAE5hACIACToaGjJCiXQgtCA=== – Chizl Apr 14 '23 at 16:13
-
Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/253157/discussion-between-chizl-and-simon). – Chizl Apr 14 '23 at 16:44