Anyone know how to programmatically mute the Windows XP Volume using C#?
Asked
Active
Viewed 3.7k times
5 Answers
16
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);

Jorge Ferreira
- 96,051
- 25
- 122
- 132
-
2Worked a treat in XP thanks. However I'm using WPF so there's no this.Handle. Instead: `public static void ToggleMute(IntPtr handle) { SendMessageW(handle, WM_APPCOMMAND, handle, (IntPtr)APPCOMMAND_VOLUME_MUTE); }` In WPF Window: `VolumeXP.ToggleMute(new WindowInteropHelper(this).Handle);` – Stephen Kennedy Sep 09 '11 at 16:48
-
1This ... worked on Windows 7 -- but I'm in a library, that is run in the background -- not in anything that will ever have a window -- I'm trying to mute another window -- this works -- but regardless of what I pass in for handle, (another application's window handle, etc.), it just mutes the master system volume... – BrainSlugs83 Sep 10 '14 at 04:23
-
Why do you use `0x80000`? `APPCOMMAND_VOLUME_MUTE` is defined as `8`? However when I use `8` it fails, but when I use your `0x80000` it works. This is odd. – Noitidart Nov 13 '16 at 19:01
-
Solution on why 0x80000 is given here - http://stackoverflow.com/a/40577881/1828637 – Noitidart Nov 13 '16 at 19:43
-
nice! you can use APPCOMMAND_VOLUME_UP 0xA0000 instead to make sure not mute like an alarm. – colin lamarre Jan 15 '21 at 21:13
6
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
}

Jimi
- 29,621
- 8
- 43
- 61

Mike de Klerk
- 11,906
- 8
- 54
- 76
-
Thank you. This library worked when all others I've tried failed. My problem was similar to this one, but not exactly the same. This is a very good find. I wish I could vote you up more than once. – David Oct 09 '12 at 15:40
-
Thanks for the upvote and feedback. I searched a while before I found an easy way to mute the sound. Therefore, I thought, spread the knowledge around on some stackoverflow questions :) I am glad it helped someone. – Mike de Klerk Oct 11 '12 at 10:33
2
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;
}
}
}
}
0
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;
}
}
}

J. Andersen
- 161
- 1
- 5
-1
CoreAudioDevice defaultPlaybackDevice = new
CoreAudioController().DefaultPlaybackDevice;
if (!defaultPlaybackDevice.IsMuted)
defaultPlaybackDevice.ToggleMute();

Guest
- 1
- 1