Mute/unmute, Change master volume in Windows 7 x64 with C#
Asked Answered
S

2

9

How can I adjust master volume in Windows 7 with C#?

I have seen an excellent implementation using winmm.dll here, but it works with XP and not with Windows 7.

Shaquana answered 6/6, 2010 at 21:43 Comment(1)
possible duplicate https://mcmap.net/q/122096/-how-to-mute-microphone-in-windows-7-with-c-cTryout
B
5

CodeProject has a very good sample here. Note that it relies on COM interop completely (check COM interface like IAudioEndpointVolume and IAudioMeterInformation on MSDN if you are interested in implementation details), and works ONLY for Vista/Win7 and higher.

Minimum supported client: Windows Vista

Minimum supported server: Windows Server 2008

Benedicite answered 6/6, 2010 at 23:45 Comment(3)
Excellent, I tested it and it worked in Windows 7 x64. Thanks a lot!Shaquana
The codeproject article has been removed :( Anyone out there who can insert the code here?Sadick
This answer contains some code (probably not the one from removed codeproject article, but at least it contains essential COM interfaces & definitions): https://mcmap.net/q/122097/-controlling-volume-mixerBenedicite
U
12

I used the nuget package Naudio successfully with this code:

public void SetVolume(int level)
   {
            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
                    {
                        if (dev.State == NAudio.CoreAudioApi.DeviceState.Active)
                        {
                            var newVolume = (float)Math.Max(Math.Min(level, 100),0) / (float)100;

                            //Set at maximum volume
                            dev.AudioEndpointVolume.MasterVolumeLevelScalar = newVolume;

                            dev.AudioEndpointVolume.Mute = level == 0;

                            //Get its audio volume
                            _log.Info("Volume of " + dev.FriendlyName + " is " + dev.AudioEndpointVolume.MasterVolumeLevelScalar.ToString());
                        }
                        else
                        {
                            _log.Debug("Ignoring device " + dev.FriendlyName + " with state " + dev.State);
                        }
                    }
                    catch (Exception ex)
                    {
                        //Do something with exception when an audio endpoint could not be muted
                        _log.Warn(dev.FriendlyName + " could not be muted with error " + ex);
                    }
                }
            }
            catch (Exception ex)
            {
                //When something happend that prevent us to iterate through the devices
                _log.Warn("Could not enumerate devices due to an excepion: " + ex.Message);
            }
        }
Untouched answered 27/4, 2015 at 8:46 Comment(0)
B
5

CodeProject has a very good sample here. Note that it relies on COM interop completely (check COM interface like IAudioEndpointVolume and IAudioMeterInformation on MSDN if you are interested in implementation details), and works ONLY for Vista/Win7 and higher.

Minimum supported client: Windows Vista

Minimum supported server: Windows Server 2008

Benedicite answered 6/6, 2010 at 23:45 Comment(3)
Excellent, I tested it and it worked in Windows 7 x64. Thanks a lot!Shaquana
The codeproject article has been removed :( Anyone out there who can insert the code here?Sadick
This answer contains some code (probably not the one from removed codeproject article, but at least it contains essential COM interfaces & definitions): https://mcmap.net/q/122097/-controlling-volume-mixerBenedicite

© 2022 - 2024 — McMap. All rights reserved.