4

I am using the following to mute/unmute the master audio on my computer. Now, I am looking for a way to determine the mute state. Is there a just as easy way to do this in C#?

    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);
John_Sheares
  • 1,404
  • 2
  • 21
  • 34
  • 1
    Check this question http://stackoverflow.com/questions/294292, in the accepted answer exist a method called `IsMuted` – RRUZ Dec 30 '10 at 02:55
  • Yes, it appears to be the answer. As stated in the comments, it only works when compatibility mode is enabled. – John_Sheares Dec 30 '10 at 04:38

3 Answers3

10

Hi just stumbled accross this old topic, but wa shaving the exact same issue.

I solved using the following:

using System.Runtime.InteropServices;

...

[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);


private const int APPCOMMAND_VOLUME_MUTE = 0x80000;
private const int APPCOMMAND_VOLUME_UP = 0xA0000;
private const int APPCOMMAND_VOLUME_DOWN = 0x90000;               
private const int WM_APPCOMMAND = 0x319;        

...

// mute (toggle)
SendMessage(this.Handle, WM_APPCOMMAND, this.Handle, (IntPtr)APPCOMMAND_VOLUME_MUTE);


// unmute
SendMessage(this.Handle, WM_APPCOMMAND, this.Handle, (IntPtr)APPCOMMAND_VOLUME_UP);
SendMessage(this.Handle, WM_APPCOMMAND, this.Handle, (IntPtr)APPCOMMAND_VOLUME_DOWN);

Mute will not always mute the audio, it's a toggle - but if you make sure to call "unmute" first you should be golden.

Best regards Kurt

Kurt Andersen
  • 109
  • 1
  • 3
  • Nice. That was quite clever. I am using it for my small open source project to mute the system volume when Windows is locked. https://github.com/dthompsonza/MuteOnLock – Dave Thompson Feb 22 '16 at 12:15
  • 1
    This is actually not an answer. The question was "How do I tell if the master volume is muted?", which is not answered here. – Matyas Mar 07 '16 at 08:04
  • This literally answers the question, but it requires that you modify the state to know it. The answer is always "no" with this solution. From there you can choose to change it or not. Of course this is only useful for some use cases. – DAG Jan 07 '20 at 17:54
0

Checkout the following tutorial. I've never played with the Mixer in C# ( or any other language ) so I'm assuming they are correct in P/Invoking the Win32 APIs and that they aren't reinventing the wheel. You can download the example and I think the method GetMixer() will do what you want.

Christopher Painter
  • 54,556
  • 6
  • 63
  • 100
0

Credit to RRUZ in comments above. See stackoverflow.com/questions/294292. The only problem is that you need compatibility mode.

John_Sheares
  • 1,404
  • 2
  • 21
  • 34