0

I made a program able to play mp3 files like this:

class Mp3Player : IDisposable
{
    public bool Repeat { get; set; }

    public Mp3Player(string fileName)
    {
        const string FORMAT = @"open ""{0}"" type mpegvideo alias MediaFile";
        string command = String.Format(FORMAT, fileName);
        mciSendString(command, null, 0, IntPtr.Zero);
    }

    public void Play()
    {
        string command = "play MediaFile";
        if (Repeat) command += " REPAT";
        mciSendString(command, null, 0, IntPtr.Zero);
    }

    public void Stop()
    {
        string command = "stop MediaFile";
        mciSendString(command, null, 0, IntPtr.Zero);
    }

    [DllImport("winmm.dll")]
    private static extern long mciSendString(string strCommand, StringBuilder strReturn, int iReturnLength, IntPtr hwndCallback);

    public void Dispose()
    {
        string command = "close MediaFile";
        mciSendString(command, null, 0, IntPtr.Zero);
    }
}

And I want to add volume bar to my media file something like this:

And if I go to the right to make the sound louder and if I go to the left otherwise.

1 Answers1

1

To Set Volume please, try something like this:

 public void SetVolume(int volume)
 {
    var command = "setaudio MediaFile volume to " + volume.ToString();
    mciSendString(command, null, 0, IntPtr.Zero);
 }

You can use values between 0 and 1000 in SetVolume

To use it in app you need to call set Volume when you change your volume control value.

Lets assume that on player form you have have Trackbar control then you need to use scroll event. For example:

private void trackBarVolume_Scroll(object sender, EventArgs e)
{
     _mp3Player.SetVolume(trackBarVolume.Value);
}

Complete Form class:


    public partial class Form1 : Form
    {
        private Mp3Player _mp3Player = new Mp3Player(@"C:\music.mp3");

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            _mp3Player.Play();
        }


        private void trackBarVolume_Scroll(object sender, EventArgs e)
        {
            _mp3Player.SetVolume(trackBarVolume.Value);
        }
    }

Screen:

form with trackbar control

Alexander V.
  • 1,518
  • 14
  • 14
  • How do I implement it how I want to? –  Jan 21 '17 at 21:44
  • now I can't hear anything I added this to my scroll event on my trackbar: `private void trackBar1_Scroll(object sender, EventArgs e) { _mp3player.SetVolume(trackBar1.Value); }` –  Jan 21 '17 at 22:16
  • You need to set minimum value to 0 and maximum value to 1000 for the trackbar – Alexander V. Jan 21 '17 at 22:17
  • still doesn't work... I even added the initial value for trackbar to be 4 so I know if it still plays but no... –  Jan 21 '17 at 22:21
  • nevermind i found the mistake... thanks for your time :) –  Jan 21 '17 at 22:23
  • 4 is too small (less then 1% of full volume) try at least a 100 – Alexander V. Jan 21 '17 at 22:24
  • do you have any idea how could I add all .mp3 files from a device into a list or something? –  Jan 21 '17 at 22:30
  • it depends on a device but one option is: http://stackoverflow.com/questions/929276/how-to-recursively-list-all-the-files-in-a-directory-in-c – Alexander V. Jan 21 '17 at 22:34