0% found this document useful (0 votes)
16 views

Mp3 Player With ListBox Using Windows Media Player in C Sharp

Uploaded by

hasnainmushtaq23
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Mp3 Player With ListBox Using Windows Media Player in C Sharp

Uploaded by

hasnainmushtaq23
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Creating Mp3 player using

Windows Media Player in C#


Description:
Creating C# Mp3 Player Using Windows Media Player:- In this tutorial I shall 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 with (.NET Framework)


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
Brows

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.
Then I take a openFileDialog from the toolbox and drag on the form it will be added below
the form for selecting files on Brows button click

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


1
2openFileDialog1.Multiselect = true;
3if (openFileDialog1.ShowDialog() == DialogResult.OK)
4{
5 files = openFileDialog1.SafeFileNames;
6 paths = openFileDialog1.FileNames;
7 for (int i = 0; i < files.Length; i++)
8 {
9 listBox1.Items.Add(files[i]);
10
11 }
12
13 }
14

Code Explanation:
openFileDialog1.Multiselect = true;
This code is used for the multiple files section

Then I set a condition to check if the

openFileDialog1.ShowDialog() == DialogResult.OK so save the name of the


file in a files = openFileDialog1.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 = openFileDialog1.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

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:
1using System.Collections.Generic;
2using System.ComponentModel;
3using System.Data;
4using System.Drawing;
5using System.Linq;
6using System.Text;
7using System.Windows.Forms;
8
9namespace MyMp3Player
10{
11 public partial class Form1 : Form
12 {
13 string[] files, paths;
14
15 public Form1()
16 {
17 InitializeComponent();
18 }
19
20
21
22 private void button1_Click(object sender, EventArgs e)
23 {
24 openFileDialog1.Multiselect = true;
25 if (openFileDialog1.ShowDialog() == DialogResult.OK)
26 {
27 files = openFileDialog1.SafeFileNames;
28 paths = openFileDialog1.FileNames;
29 for (int i = 0; i < files.Length; i++)
30 {
31 listBox1.Items.Add(files[i]);
32 }
33 }
34 }
35 private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
36 {
37 axWindowsMediaPlayer1.URL = paths[listBox1.SelectedIndex];
38 }
39 }
40}
41
42
43
44
45
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

You might also like