4

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

4

4 回答 4

4

这适用于我的 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);
        }
于 2012-09-21T16:42:09.587 回答
3

这篇相当长的文章展示了如何:在 C# 中控制音量

于 2009-05-27T12:30:52.503 回答
1

这篇 CodeProject 文章演示了如何完全控制 Windows 混音器设置,包括系统的主音量。它似乎包含了大部分可怕的 Win API 东西,所以它可能是最简单的方法。

于 2009-05-27T12:33:53.230 回答
1

简单的回答:你必须使用互操作。

我写了一个库来为你做各种声音的东西,虽然:

WinnMM.Net:http://winmm.codeplex.com/ _

于 2009-05-27T12:34:15.007 回答