jinsungy
jinsungy

Reputation: 10835

Mute Windows Volume using C#

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

Upvotes: 18

Views: 38442

Answers (5)

Guest
Guest

Reputation: 1

CoreAudioDevice defaultPlaybackDevice = new 
CoreAudioController().DefaultPlaybackDevice;   
        if (!defaultPlaybackDevice.IsMuted)
            defaultPlaybackDevice.ToggleMute();

Upvotes: -1

J. Andersen
J. Andersen

Reputation: 161

This is a slightly improved version of Mike de Klerks answer that doesn't require "on error resume next" code.

Step 1: Add the NAudio NuGet-package to your project (https://www.nuget.org/packages/NAudio/)

Step 2: Use this code:

using (var enumerator = new NAudio.CoreAudioApi.MMDeviceEnumerator())
{
    foreach (var device in enumerator.EnumerateAudioEndPoints(NAudio.CoreAudioApi.DataFlow.Render, NAudio.CoreAudioApi.DeviceState.Active))
    {
        if (device.AudioEndpointVolume?.HardwareSupport.HasFlag(NAudio.CoreAudioApi.EEndpointHardwareSupport.Mute) == true)
        {
            Console.WriteLine(device.FriendlyName);
            device.AudioEndpointVolume.Mute = false;
        }
    }
}

Upvotes: 0

Aleks
Aleks

Reputation: 492

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

Upvotes: 2

Mike de Klerk
Mike de Klerk

Reputation: 12328

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
}

Upvotes: 6

Jorge Ferreira
Jorge Ferreira

Reputation: 97849

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

Upvotes: 16

Related Questions