Controlling volume in C# using WMPLib in Windows

12,725

Solution 1

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
    }

Solution 2

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.

Share:
12,725

Related videos on Youtube

Arith
Author by

Arith

Updated on June 04, 2022

Comments

  • Arith
    Arith about 2 years

    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 control with music player. The problem is, when changing the volume property of my media player object (mplayer.settings.volume = 5;), it changes the MASTER volume. Meaning any voice feedback will be completely inaudible while music is playing. Not cool when you're driving. If I fire up windows media player, I can change the volume of the music without affecting the master volume.. so there has to be a way.

    I've thought of maybe finding out if there's an equalizer control buried in there, but the documentation on that is pathetic. -- either that or my google-fu is weak.

    So does anyone know how I would go about separating master and music volume with windows media player control?

    Particulars: Target machine is XP(sp3), with .NET 4.0 I believe. Also, this is a console app.

    Thanks in advance for any help

  • Arith
    Arith over 12 years
    I have yet to try it (busy and such) but I believe you when you say it's the only way. Given the lack of search hits on google and the fact you're the only one to reply. (and thank you for doing so)