【问题标题】:How to mute Windows with WPF?如何使用 WPF 使 Windows 静音?
【发布时间】:2010-02-28 14:13:41
【问题描述】:
我正在学习 C# 和 WPF,并且有一个小实用程序的想法。我想要一个只做一件事的红色大按钮:完全静音/取消静音所有 Windows 声音(系统哔声、WMP、DVD 播放器等)我已经在 VS 2008 中探索了对象浏览器,但不能似乎找到了我需要的东西:一个会影响所有 Windows 的静音。
是System.Windows.Input.MediaCommands.MuteVolume,我只是不知道如何使用它?
感谢任何使用 C# 和/或 WPF 的正确方向的指针。 :)
【问题讨论】:
标签:
c#
wpf
windows
volume
【解决方案1】:
我很确定各个 WPF 控件使用该命令进行静音。例如,如果 CommandTarget 是一个 MediaElement,它会在执行该命令时静音。不幸的是,我认为你将不得不做更多的工作。一个快速的谷歌给出了一些执行 p/invoke 方式的示例,这可能是目前在 .NET 中执行此操作的唯一方法:
对于 XP:MSDN
对于 Vista/7:CodeProject
【解决方案2】:
您可以使用 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
}