Controlling volume in C# using WMPLib in Windows

后端 未结 2 744
自闭症患者
自闭症患者 2021-01-16 09:33

The story: I\'m writing a music player controlled by voice. Previously the project used winamp for music -- which I\'d like to do away with. I\'d like to integrate the voice

相关标签:
2条回答
  • 2021-01-16 10:19

    The only way I found of doing this was using Interop and WM_APPCOMMAND windows message:

        private const int APPCOMMAND_VOLUME_MUTE = 0x80000;
        private const int WM_APPCOMMAND = 0x319;
        private const int APPCOMMAND_MICROPHONE_VOLUME_UP = 26 * 65536;
        private const int APPCOMMAND_MICROPHONE_VOLUME_DOWN = 25 * 65536;
    
        [DllImport("user32.dll")]
        public static extern IntPtr SendMessageW(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
        private void SetMicVolume()
        {
            SendMessageW(new WindowInteropHelper(this).Handle, WM_APPCOMMAND, new (IntPtr)APPCOMMAND_MICROPHONE_VOLUME_UP);//or _DOWN
        }
    
    0 讨论(0)
  • 2021-01-16 10:34

    I have tested this in Windows Media Player VER 12, so I guess for most people there is a much easier way than using "user32.dll":

    private static WMPLib.WindowsMediaPlayer Player;
    
    public static void VolumeUp()
    {
        if (Player.settings.volume < 90)
        {
            Player.settings.volume = (Player.settings.volume + 10);
        }
    }
    
    public static void VolumeDown()
    {
        if (Player.settings.volume > 1)
        {
            Player.settings.volume = (Player.settings.volume - (Player.settings.volume / 2));
        }
    }
    

    No doubt this has been supported for some time now. It does not change the Master Volume and only the Media Player Volume is changed. The Windows Master Volume is left alone.

    Hope this helps others out there that are not limited to XP SP3.

    0 讨论(0)
提交回复
热议问题