Creating an Mp3 Player in C#.NET Using Windows Media Component

Introduction:

In this tutorial, we will explore a straightforward method to create an Mp3 player in C#.NET. Leveraging the power of the Windows Media Component provided by Microsoft, we'll guide you through the process step by step. This beginner-friendly approach requires minimal coding, making it accessible for those new to C# development. So, let's dive in and build your own Mp3 player using Visual Studio 2012.

Mp3 Player Project in C#.NET

Step 1: Setting Up the Project

  1. Open Visual Studio.
  2. Choose "File" > "New" > "Project."
  3. Select "Visual C#" > "Windows Form Application."
Add Windows Media Player Components to Toolbar

Now, let's add the Windows Media Player component to the Toolbar.

  1. Right-click on an open space on the Toolbar.
  2. Click on "Choose Items."
  3. Under COM Components, search for "Windows Media Player," check the checkbox, and click "Ok."

Designing the Windows Application Forms

Now that our project is set up, let's design the interface for our Mp3 player.

  1. Drag and drop "openFileDialog1" and "Windows Media Player Control" from the Toolbar.
  2. Add three buttons and a ListBox to the form.

For each button, set the following properties:

  • Drag and Drop openFileDialog1 from Toolbar
  • Drag and Drop Windows Media Player Control from Toolbar
  • Add 3 Button Controls as i shown in image
    • Button 1
      • Change Property for Button 1:
      • Text : Choose Mp3 Files
      • (Name) : btn_choosemp3files
    • Button 2
      • Change Property for Button 2:
      • Text : Reset
      • (Name) : btn_Reset
    • Button 3
      • Change Property for Button 3:
      • Text : Delete
      • (Name) : btn_Delete
  • Add one ListBox Change ListBox name in property : listBoxMp3file

C# Coding : Choose Mp3 Files Button Click

Now, let's add the necessary C# code to make our Mp3 player functional.


        string[] Mp3files, Mp3path;       
        
        private void btn_choosemp3files_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Mp3files = openFileDialog1.SafeFileNames;
                Mp3path = openFileDialog1.FileNames;
                for (int i = 0; i < Mp3files.Length;i++)
                {
                    listBoxMp3Files.Items.Add(Mp3files[i]);                    
                }
            }
        }

C# Coding : Reset Button Click


        private void btn_Reset_Click(object sender, EventArgs e)
        {
            listBoxMp3Files.Items.Clear();
        }

C# Coding : Delete Button Click


private void btn_Delete_Click(object sender, EventArgs e)
        {
            listBoxMp3Files.Items.Remove(listBoxMp3Files.Items[listBoxMp3Files.SelectedIndex]);
        }

C# Coding : Double Click on Selected Item in ListBox


private void listBoxMp3Files_DoubleClick(object sender, EventArgs e)
        {
            WMP.URL = Mp3path[listBoxMp3Files.SelectedIndex];
        }

Download Mp3 Player Project with Source Code

To download the complete Mp3 player project with source code from here

Conclusion:

Congratulations! You've successfully created a simple Mp3 player in C#.NET using the Windows Media Component. This tutorial aimed to provide an easy-to-follow guide for beginners. Feel free to explore and enhance the project further, adding features like play/pause buttons, volume control, and playlist management to take your Mp3 player to the next level. Happy coding!

Post a Comment

0 Comments