3

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?

f4bzen
  • 301
  • 2
  • 6
  • 17

3 Answers3

7

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!

Abid Ali
  • 101
  • 1
  • 5
2

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);
    }
}
Community
  • 1
  • 1
  • Hi and thank you for the answer. I already saw that topic/answer but the code only gives me a bunch of errors. I´m using WindowsForms and i´m not sure if it´s because the WindowInteropHelper (or the namespace) might be limited to WPF. – f4bzen Jan 29 '13 at 00:21
  • I´m still getting a few errors on the SendMessageW-line, especially at (IntPtr)APPCOMMAND_VOLUME_UP);. The errors are for example: "'System.IntPtr' is a 'Type' but is used like a 'variable'", "Invalid expression term ')'", "')' expected", "';' expected" and "Only assignment, call, increment, decrement, and new object expressions can be used as a statement"... – f4bzen Jan 29 '13 at 19:46
  • @f4bzen, sorry! Corrected. – Sergey Vyacheslavovich Brunov Jan 29 '13 at 21:15
  • 1
    Ok, the code is now working for me, but I had to change the SendMessageW-line a little bit, since there was 1 argument missing in your version: SendMessageW(this.Handle, WM_APPCOMMAND, this.Handle, (IntPtr)APPCOMMAND_VOLUME_DOWN); So feel free to add this to your solution and thank you for helping me with this. – f4bzen Jan 29 '13 at 23:18
0

This worked for me!

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

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

//Volume 100%
finish_sound.settings.volume = 100;
M1NT
  • 386
  • 1
  • 4
  • 13