Reputation: 3641
How can I change the Windows System Sound Volume using a C# Application?
Upvotes: 69
Views: 110604
Reputation: 1896
C# code:
[Guid("5CDF2C82-841E-4546-9722-0CF74078229A"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IAudioEndpointVolume
{
int _0(); int _1(); int _2(); int _3();
int SetMasterVolumeLevelScalar(float fLevel, Guid pguidEventContext);
int _5();
int GetMasterVolumeLevelScalar(out float pfLevel);
int _7(); int _8(); int _9(); int _10(); int _11(); int _12();
}
[Guid("D666063F-1587-4E43-81F1-B948E807363F"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IMMDevice
{
int Activate(ref System.Guid id, int clsCtx, int activationParams, out IAudioEndpointVolume aev);
}
[Guid("A95664D2-9614-4F35-A746-DE8DB63617E6"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IMMDeviceEnumerator
{
int _0();
int GetDefaultAudioEndpoint(int dataFlow, int role, out IMMDevice endpoint);
}
[ComImport, Guid("BCDE0395-E52F-467C-8E3D-C4579291692E")] class MMDeviceEnumeratorComObject { }
public class Audio
{
private static readonly IAudioEndpointVolume _MMVolume;
static Audio()
{
var enumerator = new MMDeviceEnumeratorComObject() as IMMDeviceEnumerator;
enumerator.GetDefaultAudioEndpoint(0, 1, out IMMDevice dev);
var aevGuid = typeof(IAudioEndpointVolume).GUID;
dev.Activate(ref aevGuid, 1, 0, out _MMVolume);
}
public static int Volume
{
get
{
_MMVolume.GetMasterVolumeLevelScalar(out float level);
return (int)(level * 100);
}
set
{
_MMVolume.SetMasterVolumeLevelScalar((float)value / 100, default);
}
}
}
Usage:
Audio.Volume = 50;
More info on MSDN.
Upvotes: 3
Reputation: 11
You can add this library https://gist.github.com/sverrirs/d099b34b7f72bb4fb386 to your project and change the volume like this;
VideoPlayerController.AudioManager.SetMasterVolume(100);
The library also includes options for changing application volume, mute, getting current volume level etc. The namespace is called "Video Player Controller" but I used it in a Windows Forms App to change the system volume and it worked fine, so the "video" part is arbitrary.
Upvotes: 1
Reputation: 3136
I'm a bit late to the party but if you are looking now there's a nuget package available (AudioSwitcher.AudioApi.CoreAudio) that simplifies audio interactions. Install it then it’s as simple as:
CoreAudioDevice defaultPlaybackDevice = new CoreAudioController().DefaultPlaybackDevice;
Debug.WriteLine("Current Volume:" + defaultPlaybackDevice.Volume);
defaultPlaybackDevice.Volume = 80;
Upvotes: 94
Reputation: 3641
Here is the code:
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace Test
{
public class Test
{
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;
[DllImport("user32.dll")]
public static extern IntPtr SendMessageW(IntPtr hWnd, int Msg,
IntPtr wParam, IntPtr lParam);
private void Mute()
{
SendMessageW(this.Handle, WM_APPCOMMAND, this.Handle,
(IntPtr)APPCOMMAND_VOLUME_MUTE);
}
private void VolDown()
{
SendMessageW(this.Handle, WM_APPCOMMAND, this.Handle,
(IntPtr)APPCOMMAND_VOLUME_DOWN);
}
private void VolUp()
{
SendMessageW(this.Handle, WM_APPCOMMAND, this.Handle,
(IntPtr)APPCOMMAND_VOLUME_UP);
}
}
}
Found on dotnetcurry
When using WPF you need to use new WindowInteropHelper(this).Handle
instead of this.Handle
(thanks Alex Beals)
Upvotes: 57
Reputation: 21
My code is a bit different but still using CoreAudio
downloaded the pkg : nuget install AudioSwitcher.AudioApi.CoreAudio -Version 3.0.0.1
using AudioSwitcher.AudioApi.CoreAudio;
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
CoreAudioDevice defaultPlaybackDevice = new CoreAudioController().DefaultPlaybackDevice;
double vol = defaultPlaybackDevice.Volume;
defaultPlaybackDevice.Volume = defaultPlaybackDevice.Volume - 5.0;
defaultPlaybackDevice.Volume = defaultPlaybackDevice.Volume + 5.0;
}
}
Upvotes: 1
Reputation: 2647
In case you wish to set it to an exact value using the Core Audio APIs:
using CoreAudioApi;
public class SystemVolumeConfigurator
{
private readonly MMDeviceEnumerator _deviceEnumerator = new MMDeviceEnumerator();
private readonly MMDevice _playbackDevice;
public SystemVolumeConfigurator()
{
_playbackDevice = _deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia);
}
public int GetVolume()
{
return (int)(_playbackDevice.AudioEndpointVolume.MasterVolumeLevelScalar * 100);
}
public void SetVolume(int volumeLevel)
{
if (volumeLevel < 0 || volumeLevel > 100)
throw new ArgumentException("Volume must be between 0 and 100!");
_playbackDevice.AudioEndpointVolume.MasterVolumeLevelScalar = volumeLevel / 100.0f;
}
}
Upvotes: 15
Reputation: 2866
If the tutorials provided in the other answers are too involved you could try an implementation like this using the keybd_event function
[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);
Usage:
keybd_event((byte)Keys.VolumeUp, 0, 0, 0); // increase volume
keybd_event((byte)Keys.VolumeDown, 0, 0, 0); // decrease volume
Upvotes: 17