8

I got this code to mute/unmute the master volume

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

SendMessageW(this.Handle, WM_APPCOMMAND, this.Handle, (IntPtr)APPCOMMAND_VOLUME_MUTE);

I would to know how can I get the master volume level/precent because I want to know if the sound is already muted or not.

Edit: or else I would like to split the mute/unmute sound so I will have two functions - one for mute and one for unmute.

thanks

Fredrik Hedblad
  • 83,499
  • 23
  • 264
  • 266
Ron
  • 3,975
  • 17
  • 80
  • 130

3 Answers3

3

Have a look at this project http://www.codeproject.com/KB/vista/CoreAudio.aspx

They created an own mixer control, that also reports the current volumne and the mute/unmute state:

defaultDevice.AudioEndpointVolume.OnVolumeNotification += new AudioEndpointVolumeNotificationDelegate(
    AudioEndpointVolume_OnVolumeNotification);
// .. snip ..
void AudioEndpointVolume_OnVolumeNotification(AudioVolumeNotificationData data)
{
    Console.WriteLine("New Volume {0}", data.MasterVolume);
    Console.WriteLine("Muted      {0}", data.Muted);
}

Does this help you?

EDIT: With this code and the class from the project you should be able to set and unset mute directly (without toggle):

MMDeviceEnumerator devEnum = new MMDeviceEnumerator();
MMDevice defaultDevice = devEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia);
defaultDevice.AudioEndpointVolume.Mute = true; // or false
Hinek
  • 9,519
  • 12
  • 52
  • 74
  • 2
    CodeProject link is dead – Bill Tarbell Feb 18 '18 at 01:18
  • This works wonders. Note that you'll need to reference NAudio.dll with the following namespace: using NAudio.CoreAudioApi;. MasterVolumeLevelScalar is a linear 0-1 scale, whereas MasterVolumeLevel is some non-linear scale. – Bill Tarbell Feb 18 '18 at 01:53
1

I could not do it for all Windows versions (xp, vista & 7).
Though, I achieved it by used external programs, such as NirCmd, and sent the command I needed.

not so good solution but it did solve my problem.

Ron
  • 3,975
  • 17
  • 80
  • 130
0

This thread shows how to control the master volume from C#.

You might also be interested in the responses to this question: Get Master Sound Volume in c#

Especially the NAudio managed wrapper.

Community
  • 1
  • 1
Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
  • 1
    What, specifically, didn't work? Comments in the thread indicate that it works. – Jim Mischel Nov 20 '10 at 22:38
  • used the function GetVolume() and it returned 65535, after i changed the volume it returned me the same value - 65535. maybe I'm using the wrong function? – Ron Nov 20 '10 at 22:49
  • 2
    That other question is about the intensity of the currently playing sound, not the gain currently set in the mixer. – Ben Voigt Nov 21 '10 at 01:32
  • 2
    I'm still looking for an answer. none of the threads that Jim gave are working. – Ron Nov 21 '10 at 13:49