C sharp (C#)

Creating Mp3 player using Windows Media Player in C#

Description:

Hello guys welcome once again; in this article, I will show you how to make a simple mp3 player using windows media player with the help of c# sharp windows form application. Now, this is a software based mp3 player but if you want to learn how to make an mp3 player using Arduino and DFplayer then read my article on “DFplayer Mini MP3 and Arduino Based RFID Bike Access System”.



So let’s get started

First, of all click on new project

Mp3 player

Then select windows form application and set a name as you want and press ok

Mp3 player

While the Windows Forms Application is selected click on the Ok button.




Mp3 player

How to load windows media player items:

If you do not have the windows media player item in your toolbox for that simply right click on the button button in the toolbox area and click on choose item

Mp3 player

When you click on choose items a new window will open in that window click on COM Components tab, select windows media player, and press ok

Mp3 player



As you can see in the image below, the windows media player is loaded in the toolbox

Mp3 player

Drag the windows media player and just drop on the form and set the alignment of the player

Mp3 player

I will use a list box from toolbox so I will drag and drop the listbox for choosing the list of the mp3 files

Mp3 player

Then I take a button from the toolbox for choosing those files so the text of the button I set choose file

Mp3 player

This button is used for choosing the playlist  and the listbox will show the playlist of the songs  and when you select any one song from  the listbox it should play in the windows media player.



Mp3 Player Programming:

Once you have done the designing part then just double click on choose file button.

First of all declare two global variables  which are string  array type

string[] files, paths;

Then paste the below code in the button section

OpenFileDialog openFile=new OpenFileDialog();

openFile.Multiselect = true;

if (openFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)

 {

   files = openFile.SafeFileNames;

   paths = openFile.FileNames;

   for (int i = 0; i < files.Length; i++)

   {

    listBox1.Items.Add(files[i]);

                

   }

 }

OpenFileDialog openFile=new OpenFileDialog();

First i declare the openFileDialog for opening the dialogbox for selection files from the drive.

openFile.Multiselect = true;

This code is use for multiple files section

Then I set a condition to check if the

openFile.ShowDialog() == System.Windows.Forms.DialogResult.OK so save the name of the

file in a files = openFile.SafeFileNames;

files variable which we declared in top. And save the full path of the file in path variable by use the below following statement

paths = openFile.FileNames

Then I use for loop to add the songs in the listbox

for (int i = 0; i < files.Length; i++)

   {

    listBox1.Items.Add(files[i]);            

   }



Then double click on listbox and paste the below code

axWindowsMediaPlayer1.URL = paths[listBox1.SelectedIndex];

axWindowsMediaPlayre1 it is the name of the media player which I used in my application

axWindowsMediaPlayer1.URL = paths[listBox1.SelectedIndex];

so write this one line code and this one line code what is doing is it’s whatever file or whatever name you will  choose in your list box it will copy that list or the path of that file into your window media player.

Final code of MP3 Player using windows media player:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MyMp3Player
{
    public partial class Form1 : Form
    {
        string[] files, paths;

        public Form1()
        {
            InitializeComponent();
        }

        

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFile=new OpenFileDialog();
            openFile.Multiselect = true;
            if (openFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                files = openFile.SafeFileNames;
                paths = openFile.FileNames;
                for (int i = 0; i < files.Length; i++)
                {
                    listBox1.Items.Add(files[i]);
                
                }

            }

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            axWindowsMediaPlayer1.URL = paths[listBox1.SelectedIndex];
        }
    }
}



Output:

Click on choose file

Mp3 player

A file open dialog will open for files selection. Select file and press open

Mp3 player

The files will be added in the listbox and when you click on the file it will play In windows media player.

Mp3 player

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button