如何使用WPF静音Windows?

5
我正在学习C#和WPF,并想开发一个小工具。我想要一个大红按钮,只需完成一项任务:完全静音/取消静音所有Windows声音(系统提示音、WMP、DVD播放器等)。我已经在VS 2008中探索了对象浏览器,但似乎找不到所需的内容:使所有Windows静音。
是否是System.Windows.Input.MediaCommands.MuteVolume,而我只是没有理解如何使用?
谢谢任何有关使用C#和/或WPF的指导。 :)
2个回答

6
我相信该命令是由各个WPF控件用于静音的。例如,如果CommandTarget是MediaElement,则在执行该命令时会静音。不幸的是,我认为你需要做更多的工作。快速搜索可以找到一些使用p/invoke的示例,这可能是目前在.NET中唯一的方法:
对于XP:MSDN 对于Vista/7:CodeProject

1
您可以使用NAudio(http://naudio.codeplex.com/releases/view/79035)。下载最新版本。提取DLL并在C#项目中引用NAudio 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
                {
                    //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
        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接