Mute Windows Volume using C#

后端 未结 7 1323
广开言路
广开言路 2020-11-30 05:01

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

7条回答
  •  南笙
    南笙 (楼主)
    2020-11-30 05:35

    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
    }
    

提交回复
热议问题