Controlling volume in C# using WMPLib in Windows

谁都会走 提交于 2019-12-19 10:51:09

问题


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


回答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
    }



回答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.



来源:https://stackoverflow.com/questions/9203847/controlling-volume-in-c-sharp-using-wmplib-in-windows

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