问题
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);
回答1:
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
回答2:
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.
回答3:
Credit to RRUZ in comments above. See stackoverflow.com/questions/294292. The only problem is that you need compatibility mode.
来源:https://stackoverflow.com/questions/4559526/how-do-i-tell-if-the-master-volume-is-muted