change volume of mp3 playing via wmplib in c#

血红的双手。 提交于 2020-01-14 13:48:07

问题


Is it somehow possible to change the volume of a mp3-file that is playing via wmplib? Changing the volume of the program itself would be ok as well.

Are there any solutions to do this?


回答1:


The idea is to send WM_APPCOMMAND message (also see this answer).

For WPF use WindowInteropHelper to get the Handle of the Window:

class MainWindow : Window
{
    ...

    private const int APPCOMMAND_VOLUME_MUTE = 0x80000;
    private const int WM_APPCOMMAND = 0x319;
    private const int APPCOMMAND_VOLUME_UP = 10 * 65536;
    private const int APPCOMMAND_VOLUME_DOWN = 9 * 65536;

    [DllImport("user32.dll")]
    public static extern IntPtr SendMessageW(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);

    private void VolumeUp()
    {
        // APPCOMMAND_VOLUME_UP or APPCOMMAND_VOLUME_DOWN
        var windowInteropHelper = new WindowInteropHelper(this);
        SendMessageW(windowInteropHelper.Handle, (IntPtr)WM_APPCOMMAND, windowInteropHelper.Handle, (IntPtr)APPCOMMAND_VOLUME_UP);
    }
}

For Windows Forms use Control.Handle Property:

class MainForm : Form
{
    ...

    private void VolumeUp()
    {
        SendMessageW(Handle, (IntPtr)WM_APPCOMMAND, Handle, (IntPtr)APPCOMMAND_VOLUME_UP);
    }
}



回答2:


This is a simple way to do it.

Example:

WMPlib.WindowsMediaPlayer wmp = new WMPlib.WindowsMediaPlayer(); //Creates an instance of the WMP
wmp.url="URI to media source"; //Sets media source
wmp.settings.volume= 50;  //Volume can be 0-100 (inclusive)

Hope it helped you!




回答3:


This worked for me!

WMPLib.WindowsMediaPlayer wmsound= new WMPLib.WindowsMediaPlayer();

wmsound.URL = @"C:\Users\USER\sound.mp3";

//Volume 100%
finish_sound.settings.volume = 100;


来源:https://stackoverflow.com/questions/14527339/change-volume-of-mp3-playing-via-wmplib-in-c-sharp

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