Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I tell if the master volume is muted?

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);
like image 265
John_Sheares Avatar asked Dec 29 '22 03:12

John_Sheares


1 Answers

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

like image 177
Kurt Andersen Avatar answered Jan 12 '23 02:01

Kurt Andersen