在 C# 中静音麦克风

问题描述

我目前正在尝试用 C# 编写一个程序,该程序可以使麦克风静音和取消静音,以模拟口吃效果。 我试过谷歌搜索,但我能找到的只是一些 10 年前的条目。 我尝试使用 MediaCommands 然后使用 MicrophoneVolumeMute 但无法让它工作。 提前致谢

解决方法

有许多用纯 C# 编写的开源项目完全可以完成这项工作。例如,您可以查看 https://github.com/DanielGilbert/dgMicMute。不幸的是,自从 Win7 以来,全局切换麦克风不再那么容易了。因此,所需的代码相当可观。作为起点,您可以阅读DgMic.cs

如果我们考虑使用外部库,事情就会变得容易得多。下面是一些适用于 NAudio 的代码:

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

要完成这项工作,您必须添加对 NAudio NuGet package 的引用。