使用C#静音Windows卷

任何人都知道如何使用C#以编程方式静音Windows XP卷?

为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); 

然后使用此线来静音/取消静音。

 SendMessageW(this.Handle, WM_APPCOMMAND, this.Handle, (IntPtr) APPCOMMAND_VOLUME_MUTE); 

你可以用于Windows Vista / 7以及可能还有8个:

您可以使用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 } 

如果您正在运行Vista,我偶然发现了这个可能感兴趣的项目 。

请参阅如何使用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; } } 

您可能希望使用MCI命令: http : //msdn.microsoft.com/en-us/library/ms709461(VS.85).aspx

我应该补充一点,虽然这会让你对windows中的输入和输出混音器有很好的控制,但你可能在进行详细控制方面遇到一些困难,例如设置麦克风增强等。

哦,如果你在Vista上,那就忘了吧。 这是一个完全不同的模型。

你可以使用P / Invoke,如下所述: http : //www.microsoft.com/indonesia/msdn/pinvoke.aspx 。 它实际上经历了任务1:靠近顶部静音和取消静音的步骤。