Controlling volume in C# using WMPLib in Windows

南笙酒味 提交于 2019-12-01 13:22:11

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
    }
Rusty Nail

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!