csharp

Creating Mp3 player using Windows Media Player in C#

Description:

Creating C# Mp3 Player Using Windows Media Player:- Hello, guys welcome once again in this article I will show you how to make a simple C# mp3 player using a windows media player with the help of your c# windows form application.

So let’s get started

First, of all click on the new project

Mp3 player

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

Mp3 player



When you click ok the below interface will be open

Mp3 player

How to load windows media player items:

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

Mp3 player

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

Mp3 player

As you can see 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 the 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

The button is using 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.


C# Mp3 Player Programming:

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

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

Then paste the below code in the button section

Code Explanation:

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 used for the 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 the top. And save the full path of the file in the path variable by using the below  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 for making mp3 player

axWindowsMediaPlayer1.URL = paths[listBox1.SelectedIndex];

so write the above 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  C# MP3 Player using windows media player:

Output:

Click on choose file

Mp3 player

A file open dialog will be open for file selection. Select files 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

Related Article:

C# Windows Forms Application: How to create First Window Form Application in C#

Related Articles

Leave a Reply

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

Back to top button