Volume control in C # using WMPLib on Windows

History: I am writing a voice controlled music player. Previously, the project used winamp for music and I would like to end that. I would like to integrate voice control with a music player. The problem is that when I change the volume property of my media player object (mplayer.settings.volume = 5;), it changes the MASTER volume. The meaning of any spoken feedback will be completely inaudible during music playback. Not cool when you drive. If I start Windows media player, I can change the music volume without affecting the master volume, so there must be a way.

I thought I might figure out if there is an EQ control there, but the documentation on this is pathetic. - either this or my google-fu is weak.

Does anyone know how I would like to share master and volume of music with windows media player control?

Particulars: The target computer is XP (sp3), with .NET 4.0 I believe. It is also a console application.

Thanks in advance for your help

+1


source to share


2 answers


The only way I have found this is using Windows Interop and WM_APPCOMMAND messages:



    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
    }

      

+2


source


I tested this in Windows Media Player VER 12, so I suppose there is a much easier way for most people 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));
    }
}

      



Undoubtedly, this has been supported for some time. It does not change the master volume and only the volume of the media player changes. The Windows master volume remains alone.

Hope this helps other people who are not limited to XP SP3.

+3


source







All Articles