Mp3 Player in C#

Mp3 Player in C# can be created without using a media control. In most of tutorials you’ve learned designing Mp3 player in C# using Windows Media Player control. But in this tutorial we are not going to use that approach.

Tools Required:

  1. Visual Studio 2010 or Later version.
  2. Pichon desktop app (Optional) for icons. Download it from Here

Steps to Follow:

  1. After creating a new project in Windows Forms App in Visual Studio right click on project file and Add a new class file.
  2. Name that class as Mp3Player.cs and add the following code.
[DllImport("winmm.dll")]
        private static extern long mciSendString(string lpstrCommand, StringBuilder lpstrReturnString,int uReturnLength,int hwdCallBack);

        public void open(string File)
        {
            string Format = @"open ""{0}"" type MPEGVideo alias MediaFile";
            string command = string.Format(Format,File);
            mciSendString(command, null, 0, 0);
        }
        
        public void play()
        {
            string command = "play MediaFile";
            mciSendString(command, null, 0, 0);
        }
        public void stop()
        {
            string command = "stop MediaFile";
            mciSendString(command, null, 0, 0);
        }

In the above code, mciSendString will handle our commands to Load, Play or Stop our audio file.

Open method will load and prepare the audio file. The play and stop method can be used to play or stop the audio file respectively.

Open the design view of your form and add three buttons to open, play and stop the audio file.

Double click on Open button to create OnClick Event. But before adding the code inside this event, First of all create an instance of Mp3Player class on the top of Form1.cs class like,

private Mp3Player mp3Player = new Mp3Player();

and add the follwing code inside the onClick event of Open button:

using (OpenFileDialog ofd = new OpenFileDialog())
{
ofd.Filter = “Mp3 Files|*.mp3”;
if (ofd.ShowDialog() == DialogResult.OK)
{
mp3Player.open(ofd.FileName);
}
}

This will open a file dialog box to select your mp3 file.

Now go to the On Click event of Play Button and call the play method of Mp3Player class.

mp3Player.play();

Now go to the click event of stop button and call the stop method of Mp3Player class.

mp3Player.stop();

Video Tutorial:

Video tutorial

Source Code:

I hope you enjoyed this tutorial. Thanks for visiting us.

Please don’t forget to subscribe our official YouTube Channel C# Ui Academy

11 Replies to “Mp3 Player in C#”

  1. Sir, I’m not able to play song.The file selection is working fine but the play button isn’t working…

  2. It’s hard to find knowledgeable people on this topic, but you sound like you know what you’re talking about! Thanks

  3. mantap, postingan ini sangat menolong. ane jadi mengerti banyak hal dari postingan ini. cool!!!

  4. keren, tulisan ini sangat membantu. aku jadi mengetahui banyak hal dari web ini. good job!!!

  5. maknyus, postingan ini sangat mudah dimengerti. saya jadi mengetahui banyak hal dari artikel ini. cool!!!

  6. mantap, blog ini sangat menolong. ane jadi mengerti banyak hal dari web ini. cool!!!

  7. Pretty amazing post. I stumbled upon your blog and wished to say that I’ve really enjoyed reading your blog post. After all I’ll be subscribing to your rss feed and I hope you write again soon!

  8. Pretty nice post. I stumbled upon your post and wished to say that I’ve really enjoyed reading your blog post. After all I’ll be subscribing to your rss feed and I hope you write again soon!

  9. Magnificent items from you, man. I have consider your stuff previous to and you are simply too great. I actually like what you have got here, really like what you are stating and the way in which during which you say it. You’re making it entertaining and you still take care of to stay it smart. I can’t wait to read much more from you. This is really a great site.

  10. cool post. I just stumbled upon your post and wished to say that I’ve really enjoyed reading your blog post. After all I’ll be subscribing to your rss feed and I hope you write again soon!

Comments are closed.