Pages

Monday, 24 March 2014

Music Player in C#


Hi All,








Here you can learn to Create a Music Player in C#. This is a simple tutorial and even beginners can learn without any confusion.

This Player also supports video files (large files are not supported)


Lets Get Started 

1. Open Visual Studio -> New Project -> Windows Form Application and name it as
    MusicPlayer.




2. Your New Project will be created with a blank Form. You can Run it by clicking on the
    green Run/Start button on the top. Now change the name of your form by clicking on
    the form and press F4 to get the Properties dialog box. Here change the Text to
    "Music Player". If you want, change the BackColor of your form to get a different 
     background color for your form. 

3. Now add 4 buttons, 1 textbox , 1 label and an OpenFileDialog to your form from the
    toolbox (shown in the left side of Visual Studio) and design it as shown in the below
    image.  (To add controls to a form, just click on the control from the toolbox and drag
    it to the form).



   Click on each control and change its Properties as shown below;

    button1 :  Name -  btnOpen
                    Text  -  Open
    button2 :  Name -  btnPlay
                   Text   -  Play
    button3 :  Name -  btnPause
                    Text  -   Pause
    button4 :  Name -  btnStop
                    Text  -   Stop
    label1    :  Text  -   Now Playing
    textbox1 : Name -  txtbxFilepath

4. Now Add a new Class to your Project. To do that Right Click on your Project from the 
    Solution Explorer and Add -> New Item -> Class and name it as Music.


5. Now its time to start the coding part of our project. Update the code of your newly
    created class Music.cs with the below given code.

    Music.cs

using System.Runtime.InteropServices;
using System.Text;
namespace MusicPlayer
{
    class Music
    {
          //To import the dll winmn.dll which allows to play mp3 files
        [DllImport("winmm.dll")]
        private static extern long mciSendString(string lpstrCommand, StringBuilder lpstrReturnString,
            int uReturnLength, int hwndCallback);

       //To open a file to our application
        public void open(string file)
        {
            string command = "open \"" + file + "\" type MPEGVideo alias MyMusic";
            mciSendString(command, null, 0, 0);
        }

        //To play the file
        public void play()
        {
            string command = "play MyMusic";
            mciSendString(command, null, 0, 0);
        }

        //To pause the file
        public void pause()
        {
            string command = "stop MyMusic";
            mciSendString(command, null, 0, 0);
            
        }
       
        //To stop the file
        public void stop()
        {
            string command = "close MyMusic";
            mciSendString(command, null, 0, 0);
        }
    }
}

6. Now double click on Open button from your form. Event handler for Open button click
   will be added as shown below.

  
   Now do the same for all other buttons (Play, Pause and Stop).  Then update the code 
   as shown below

using System;

using System.Windows.Forms;

namespace MusicPlayer
{
    public partial class Form1 : Form
    {
        Music m = new Music();
        public Form1()
        {
            InitializeComponent();
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                m.open(openFileDialog1.FileName);
                txtbxFilepath.Text = openFileDialog1.FileName;
            }
        }

        private void btnPlay_Click(object sender, EventArgs e)
        {
            m.play();
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            m.stop();
            txtbxFilepath.Text = "";
           
        }

        private void btnPause_Click(object sender, EventArgs e)
        {
            m.pause();
        }
    }
}


N.B :- Always try to add event handlers by double clicking on the controls.Just   
          typing / copying the code will not add the code that hooks up the 
          corresponding events to the handlers.

7. Now Rebuild and Run your project. See your Music Player is ready and you can open 
    your music file / video file and play it, pause and Stop.

    Output

    1. Music


    2. Video





8. Enjoy the Music. In case of any errors or doubts please feel free to drop in a comment.
    I am happy to help you.



See Also




5 comments:

  1. Love this code! Now how can I get videos to play in a control in a WinForm instead of opening and playing in its own window.

    ReplyDelete
  2. where the code below step 6 is to be written in program

    ReplyDelete
  3. where the code below step 6 is to be written in program

    ReplyDelete
  4. i having problem in [DllImport]

    ReplyDelete
  5. Hey,
    Nice tutorial.But I could not get this code working. I think the problem lies with the DLL import,please could you explain it?

    CHeers

    ReplyDelete

 

About Me

I am Tijo Tom, a Computer Engineer from India.Right from my schooling, I had a passion for Programming, PC Tricks, Virus Creation and Hacking.

I started this blog on Jan 1, 2014 to share my views and ideas with the world.

You can know more about me on