.net - 从 .net 代码更改音量

标签 .net audio volume

有没有一种简单的方法可以从托管的 .net 代码中设置音量?

最佳答案

这适用于我的 Windows 7:

下载 NAudio (http://naudio.codeplex.com/releases/view/79035) 并在您的项目中引用 DLL。然后添加以下代码:

        try
        {
            //Instantiate an Enumerator to find audio devices
            NAudio.CoreAudioApi.MMDeviceEnumerator MMDE = new NAudio.CoreAudioApi.MMDeviceEnumerator();
            //Get all the devices, no matter what condition or status
            NAudio.CoreAudioApi.MMDeviceCollection DevCol = MMDE.EnumerateAudioEndPoints(NAudio.CoreAudioApi.DataFlow.All, NAudio.CoreAudioApi.DeviceState.All);
            //Loop through all devices
            foreach (NAudio.CoreAudioApi.MMDevice dev in DevCol)
            {
                try
                {
                    //Set at maximum volume
                    dev.AudioEndpointVolume.MasterVolumeLevel = 0;

                    //Get its audio volume
                    System.Diagnostics.Debug.Print("Volume of " + dev.FriendlyName + " is " + dev.AudioEndpointVolume.MasterVolumeLevel.ToString());

                    //Mute it
                    dev.AudioEndpointVolume.Mute = true;
                    System.Diagnostics.Debug.Print(dev.FriendlyName + " is muted");
                }
                catch (Exception ex)
                {
                    //Do something with exception when an audio endpoint could not be muted
                    System.Diagnostics.Debug.Print(dev.FriendlyName + " could not be muted");
                }
            }
        }
        catch (Exception ex)
        {
            //When something happend that prevent us to iterate through the devices
            System.Diagnostics.Debug.Print("Could not enumerate devices due to an excepion: " + ex.Message);
        }

关于.net - 从 .net 代码更改音量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/915454/

相关文章:

c# - 解释 expression.Compile()() 中的 2 对括号

javascript - 将音频文件保存在 Rails 中

c# - 从代码中静音系统

Javascript:你能读懂系统卷吗?

.net - 如何在Powershell中将十六进制值转换为版本号

c# - 如何在没有url编码的情况下允许&在带有西类牙字符的url中的单词之间

google-chrome - 音频下载链接在 Firefox 中有效,但在 Chrome 中流式传输

audio - 如何将Praat脚本应用于音频文件?

iphone - AudioQueue读取输入音量

c# - ASP.NET MVC4 Web API 接口(interface)作为数据契约