Mute Windows Volume using C#

こ雲淡風輕ζ 提交于 2019-12-17 09:32:23

问题


Anyone know how to programmatically mute the Windows XP Volume using C#?


回答1:


Declare this for 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);



回答2:


What you can use for Windows Vista/7 and probably 8 too:

You can use NAudio.
Download the latest version. Extract the DLLs and reference the DLL NAudio in your C# project.

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
}



回答3:


I came across this project that might be of interest, if you're running Vista.




回答4:


See How to programmatically mute the Windows XP Volume using C#?

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;
            }
        }
    }
}



回答5:


You will probably want to use MCI commands: 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.

Oh, and if you're on Vista, then just forget it. It's a totally different model.




回答6:


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.



来源:https://stackoverflow.com/questions/154089/mute-windows-volume-using-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!