繁体   English   中英

如何使用WPF静音Windows?

[英]How to mute Windows with WPF?

我正在学习C#和WPF并且有一个小实用程序的想法。 我想要一个大红色按钮,只做一件事:完全静音/取消静音所有Windows声音(系统蜂鸣声,WMP,DVD播放器等等)我在VS 2008中探索过对象浏览器但是不能似乎找到了我需要的东西:一个会影响所有Windows的静音。

System.Windows.Input.MediaCommands.MuteVolume ,我只是没有得到如何使用它?

感谢使用C#和/或WPF在正确方向上的任何指针。 :)

我很确定这个命令被各个WPF控件用于静音。 例如,如果CommandTarget是MediaElement,它将在执行该命令时将其声音静音。 不幸的是,我认为你将不得不做更多的工作。 一个快速的谷歌提供了一些用于执行p / invoke方式的示例,这可能是目前在.NET中执行此操作的唯一方法:

对于XP: MSDN

对于Vista / 7: CodeProject

您可以使用NAudio(http://naudio.codeplex.com/releases/view/79035)。 下载最新版本。 解压缩DLL并在C#项目中引用DLL NAudio。

然后添加以下代码以迭代所有可用的音频设备并尽可能将其静音。

        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
                {
                    //Show us the human understandable name of the device
                    System.Diagnostics.Debug.Print(dev.FriendlyName);
                    //Mute it
                    dev.AudioEndpointVolume.Mute = true;
                }
                catch (Exception ex)
                {
                    //Do something with exception when an audio endpoint could not be muted
                }
            }
        }
        catch (Exception ex)
        {
            //When something happend that prevent us to iterate through the devices
        }

暂无
暂无

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

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