0

i searched like hours, now i ask in this Forum.

How can I control the System Volume Setting of Windows 10?

Which Libary I need?

I am using Visual Basic 2015 and wanna programm a Windows Universal App with C#.

The programm should be able to:

  • Set Systemvolume to x%

  • increase the Systemvolume by x

  • decrease the Systemvolume by x

  • get the current Systemvolume

I found a similar Question and Answer, but the Answer doesent work.

private void Mute() {

        SendMessageW(new WindowInteropHelper(this).Handle, WM_APPCOMMAND, new WindowInteropHelper(this).Handle,
            (IntPtr)APPCOMMAND_VOLUME_MUTE);
    }

it can't find "WindowInteropHelper". But I implement:

using System;

using System.Windows.Forms;

using System.Runtime.InteropServices;

Michael Koch
  • 51
  • 1
  • 3
  • 2
    Duplicate of [How to programmatically set the system volume?](https://stackoverflow.com/questions/13139181/how-to-programmatically-set-the-system-volume) – Herohtar Jan 11 '20 at 07:19

3 Answers3

4
class VolumeChanger
{
  private const byte VK_VOLUME_MUTE = 0xAD;
  private const byte VK_VOLUME_DOWN = 0xAE;
  private const byte VK_VOLUME_UP = 0xAF;
  private const UInt32 KEYEVENTF_EXTENDEDKEY = 0x0001;
  private const UInt32 KEYEVENTF_KEYUP = 0x0002;

  [DllImport("user32.dll")]
  static extern void keybd_event(byte bVk, byte bScan, UInt32 dwFlags, UInt32 dwExtraInfo);

  [DllImport("user32.dll")]
  static extern Byte MapVirtualKey(UInt32 uCode, UInt32 uMapType);

  public static void VolumeUp()
  {
     keybd_event(VK_VOLUME_UP, MapVirtualKey(VK_VOLUME_UP, 0), KEYEVENTF_EXTENDEDKEY, 0);
     keybd_event(VK_VOLUME_UP, MapVirtualKey(VK_VOLUME_UP, 0), KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
  }

  public static void VolumeDown()
  {
     keybd_event(VK_VOLUME_DOWN, MapVirtualKey(VK_VOLUME_DOWN, 0), KEYEVENTF_EXTENDEDKEY, 0);
     keybd_event(VK_VOLUME_DOWN, MapVirtualKey(VK_VOLUME_DOWN, 0), KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
  }

  public static void Mute()
  {
     keybd_event(VK_VOLUME_MUTE, MapVirtualKey(VK_VOLUME_MUTE, 0), KEYEVENTF_EXTENDEDKEY, 0);
     keybd_event(VK_VOLUME_MUTE, MapVirtualKey(VK_VOLUME_MUTE, 0), KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
  }
}

Using this, you can mute, and increase or decrease Systemvolume by 2 degree.

I still searching a way to get the current Systemvolume.

pingze
  • 973
  • 2
  • 9
  • 18
  • remember that using Mute(), will Mute OR Unmute, depending of the current state of the audio! – Lotan May 09 '20 at 19:32
1

You can't do that. Universal apps are sandboxed and can't make global modifications to the system. This includes the system volume.

Kevin Gosse
  • 38,392
  • 3
  • 78
  • 94
  • Could I use a Script or something else, that solve my Problem? Could a Universal Windows App interact with "this" Script? – Michael Koch Feb 24 '16 at 09:11
  • I don't want to entirely rule it out, but I'm almost certain it's impossible to run a script, as it'd defeat the whole point of having a sandbox – Kevin Gosse Feb 24 '16 at 09:51
  • That said, if a service was running on the machine, you could definitely interact with this service. That may be a way out – Kevin Gosse Feb 24 '16 at 09:53
0

I believe there is a way using nircmd.

First download nircmd and attach it to the project:

http://www.nirsoft.net/utils/nircmd.html

Then, call it via cmd:

Run Command Prompt Commands

The commands you want are specified in the nircmd website.

for instance, to change the volume to x% use:

realativePath/nircmd.exe setsysvolume x

Community
  • 1
  • 1
Idan Levi
  • 388
  • 3
  • 18