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

发布于 2024-09-04 16:20:25 字数 235 浏览 3 评论 0 原文

如何使用 C# 在 Windows 7 中调节主音量?

我已经看到使用 winmm.dll 此处,但它适用于 XP,不适用于 Windows 7。

How can I adjust master volume in Windows 7 with C#?

I have seen an excellent implementation using winmm.dll here, but it works with XP and not with Windows 7.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如歌彻婉言 2024-09-11 16:20:25

我通过以下代码成功使用了 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);
            }
        }

I used the nuget package Naudio successfully with this code:

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);
            }
        }
哭了丶谁疼 2024-09-11 16:20:25

CodeProject 在此处提供了一个非常好的示例。请注意,它完全依赖于 COM 互操作(检查 COM 接口,如 IAudioEndpointVolumeIAudioMeterInformation如果您对实现细节感兴趣,请访问 MSDN),并且仅适用于 Vista/Win7 及更高版本。

最低支持的客户端:Windows Vista

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

CodeProject has a very good sample here. 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.

Minimum supported client: Windows Vista

Minimum supported server: Windows Server 2008

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文