0

I'm making a game for a school assignment. With background music that uses WMP and one of the specifications is To turn off the background music. When entering one of the game modes. The way the game is coded is that It opens a new window for the specific. Game mode. But I'm unable to Pause/stop The music windows specific Windows are open From my basic knowledge of programming, it's probably using an IF Statement that when. The specific window is open, the music stops. That's my best idea,

  • Does this answer your question? [Controlling Application's Volume: By Process-ID](https://stackoverflow.com/questions/20938934/controlling-applications-volume-by-process-id) and [Send key “MediaPlayPause” to an application without setting focus to it](https://stackoverflow.com/questions/7199203/send-key-mediaplaypause-to-an-application-without-setting-focus-to-it) –  May 25 '21 at 12:20

1 Answers1

0
static class MediaMixer
{

  static public void StopPlaying()
  {
    var input = new NativeMethods.INPUT { Type = 1 };
    input.Data.Keyboard = new NativeMethods.KEYBDINPUT
    {
      Vk = 0xB2,
      Scan = 0,
      Flags = 0,
      Time = 0,
      ExtraInfo = IntPtr.Zero
    };
    var inputs = new NativeMethods.INPUT[] { input };
    NativeMethods.SendInput(1, inputs, Marshal.SizeOf(typeof(NativeMethods.INPUT)));
  }

  static public void MuteVolume(IntPtr handle)
  {
    NativeMethods.SendMessageW(handle.Value,
                               NativeMethods.WM_APPCOMMAND,
                               handle.Value,
                               (IntPtr)NativeMethods.APPCOMMAND_VOLUME_MUTE);
  }


}
static class NativeMethods
{

  public const int WM_APPCOMMAND = 0x319;
  public const int APPCOMMAND_VOLUME_MUTE = 0x80000;

  [StructLayout(LayoutKind.Sequential)]
  public struct INPUT
  {
    public uint Type;
    public MOUSEKEYBDHARDWAREINPUT Data;
  }

  [StructLayout(LayoutKind.Sequential)]
  public struct KEYBDINPUT
  {
    public ushort Vk;
    public ushort Scan;
    public uint Flags;
    public uint Time;
    public IntPtr ExtraInfo;
  }

  [DllImport("user32.dll", SetLastError = true)]
  static public extern uint SendInput(uint numberOfInputs, 
                                      INPUT[] inputs,
                                      int sizeOfInputStructure);

}