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
Then select windows form application and set a name as you want and press ok
When you click ok the below interface will be open
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
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
As you can see 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 the 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
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
1 |
string[] files, paths; |
Then paste the below code in the button section
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
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]); } } |
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:
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 |
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 be open for file selection. Select files 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
Related Article:
C# Windows Forms Application: How to create First Window Form Application in C#