简体   繁体   English

静音/取消静音,使用C#在Windows 7 x64中更改主音量

[英]Mute/unmute, Change master volume in Windows 7 x64 with C#

How can I adjust master volume in Windows 7 with C#? 如何使用C#在Windows 7中调整主音量?

I have seen an excellent implementation using winmm.dll here , but it works with XP and not with Windows 7. 我一直在使用WINMM.DLL看到一个很好的实现在这里 ,但它与XP和与Windows 7不工作。

I used the nuget package Naudio successfully with this code: 我用这段代码成功使用了nuget包Naudio:

public void SetVolume(int level)
   {
            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
                    {
                        if (dev.State == NAudio.CoreAudioApi.DeviceState.Active)
                        {
                            var newVolume = (float)Math.Max(Math.Min(level, 100),0) / (float)100;

                            //Set at maximum volume
                            dev.AudioEndpointVolume.MasterVolumeLevelScalar = newVolume;

                            dev.AudioEndpointVolume.Mute = level == 0;

                            //Get its audio volume
                            _log.Info("Volume of " + dev.FriendlyName + " is " + dev.AudioEndpointVolume.MasterVolumeLevelScalar.ToString());
                        }
                        else
                        {
                            _log.Debug("Ignoring device " + dev.FriendlyName + " with state " + dev.State);
                        }
                    }
                    catch (Exception ex)
                    {
                        //Do something with exception when an audio endpoint could not be muted
                        _log.Warn(dev.FriendlyName + " could not be muted with error " + ex);
                    }
                }
            }
            catch (Exception ex)
            {
                //When something happend that prevent us to iterate through the devices
                _log.Warn("Could not enumerate devices due to an excepion: " + ex.Message);
            }
        }

CodeProject has a very good sample here . CodeProject 在这里有一个非常好的示例。 Note that it relies on COM interop completely (check COM interface like IAudioEndpointVolume and IAudioMeterInformation on MSDN if you are interested in implementation details), and works ONLY for Vista/Win7 and higher. 请注意,它完全依赖于COM互操作(如果您对实现细节感兴趣,请在MSDN上检查COM接口,如IAudioEndpointVolumeIAudioMeterInformation ),并且仅适用于Vista / Win7及更高版本。

Minimum supported client: Windows Vista 支持的最低客户端:Windows Vista

Minimum supported server: Windows Server 2008 支持的最低服务器:Windows Server 2008

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM