使用 C# 静音 Windows 音量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/154089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mute Windows Volume using C#
提问by jinsungy
Anyone know how to programmatically mute the Windows XP Volume using C#?
任何人都知道如何使用 C# 以编程方式将 Windows XP Volume 静音?
回答by jinsungy
You will probably want to use MCI commands: http://msdn.microsoft.com/en-us/library/ms709461(VS.85).aspx
您可能想要使用 MCI 命令:http: //msdn.microsoft.com/en-us/library/ms709461(VS.85).aspx
I should add that while this will give you good general control over the input and output mixers in windows, you may have some difficulty with doing detailed controls, like setting the mic boost, etc.
我应该补充一点,虽然这可以让您对 Windows 中的输入和输出混音器进行良好的总体控制,但您可能会在进行详细控制时遇到一些困难,例如设置麦克风增强等。
Oh, and if you're on Vista, then just forget it. It's a totally different model.
哦,如果您使用的是 Vista,那就忘记它吧。这是一个完全不同的模型。
回答by Jim B-G
You could use P/Invoke as explained here: http://www.microsoft.com/indonesia/msdn/pinvoke.aspx. It actually goes through the steps in Task 1: Mute and Unmute Sound near the top.
您可以按照此处的说明使用 P/Invoke:http: //www.microsoft.com/indonesia/msdn/pinvoke.aspx。它实际上经历了任务 1:顶部附近的静音和取消静音中的步骤。
回答by itsmatt
I came across this projectthat might be of interest, if you're running Vista.
回答by Jorge Ferreira
Declare this for P/Invoke:
为 P/Invoke 声明:
private const int APPCOMMAND_VOLUME_MUTE = 0x80000;
private const int WM_APPCOMMAND = 0x319;
[DllImport("user32.dll")]
public static extern IntPtr SendMessageW(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
And then use this line to mute/unmute the sound.
然后使用此行将声音静音/取消静音。
SendMessageW(this.Handle, WM_APPCOMMAND, this.Handle, (IntPtr) APPCOMMAND_VOLUME_MUTE);
回答by Mike de Klerk
What you can use for Windows Vista/7 and probably 8 too:
你可以在 Windows Vista/7 和 8 上使用什么:
You can use NAudio.
Download the latest version. Extract the DLLs and reference the DLL NAudio in your C# project.
您可以使用NAudio。
下载最新版本。提取 DLL 并在 C# 项目中引用 DLL NAudio。
Then add the following code to iterate through all available audio devices and mute it if possible.
然后添加以下代码以遍历所有可用的音频设备并尽可能将其静音。
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
}
回答by Aleks
See How to programmatically mute the Windows XP Volume using C#?
请参阅如何使用 C# 以编程方式将 Windows XP 卷静音?
void SetPlayerMute(int playerMixerNo, bool value)
{
Mixer mx = new Mixer();
mx.MixerNo = playerMixerNo;
DestinationLine dl = mx.GetDestination(Mixer.Playback);
if (dl != null)
{
foreach (MixerControl ctrl in dl.Controls)
{
if (ctrl is MixerMuteControl)
{
((MixerMuteControl)ctrl).Value = (value) ? 1 : 0;
break;
}
}
}
}