Creating Mp3 player using Windows Media Player in C#
Table of Contents
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
Then select windows form application and set a name as you want and press ok
While the Windows Forms Application is selected click on the Ok button.
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
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
As you can see in the image below, the windows media player is loaded in the toolbox
Drag the windows media player and just drop on the form and set the alignment of the 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
Then I take a button from the toolbox for choosing those files so the text of the button I set choose file
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
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
A file open dialog will open for files selection. Select file and press open
The files will be added in the listbox and when you click on the file it will play In windows media player.