How To Build A Windows Media Player Using Windows Forms.(.NET Framework)

Cyril Owuor
4 min readJan 10, 2022
Windows Media Player

For a long time I loved music and different genres really captivated how my mood for the day would go. So I decided one day why not relive the old days of the Windows Media player. The first version of Windows Media Player appeared in 1991, when windows 3.0 with Multimedia Extension was released.

PREQUSITES

  1. Visual Studio 2019/2022.
  2. .NET SDK 3.1/5.0/6.0.
  3. Knowledge in C# Programming.
  4. Knowledge in fundamentals of Programming.

STEPS FOR BUILDING A WINDOWS MEDIA PLAYER

Step 1

Create a new Project in Visual Studio Code

First, open File -> New-> Project in Visual Studio:

Select Windows Forms App(.NET Framework), give a name to your project and click Create.

An empty form will be created. Click Toolbox -> Components. Right click components and choose items. A window of Choose Toolbox Items will be displayed. Click COM Components and tick the check box of Windows Media Player. It will be displayed in the components item if it was not already preset.

Adding the Windows Media Player component
Windows media player component added to the Toolbox

Step 2

Design the Window Form.

Choose the Window Media player component and position it in the form. Next choose the List Box component add it to the form. Finally choose the Button component and add it to the form as shown below. In the properties section change the text of the button to Choose File. This is the button which will be used to choose the music format to load in the MP3 Player.

Form Design

Step 3

Code Structure

In the Solution Explorer, right click the project and select Add, then click add class. Name the class MediaFile.cs to store the Filename and Path.

Add Class
Media File Class

Double click on the form to go to the code structure of the form (Form.cs)and write the following code.

Form method code

Next we click on the button and add the following code:

Button Code

Next we click on the listbox and add the following code

ListBox Code

Finally we add the code for the windows media player

Windows media player code

Lets run the code:

Click Start and select the music from your preferred storage point.

Music gallery
MP3 player

It can better be illustrated by the following code:

Gist code for MP3 Player

Code Explanation

using (OpenFileDialog openFile = new OpenFileDialog() { Multiselect = true, ValidateNames = true, Filter = “MP4|*.mp4|MP3|*.mp3|WMV|* .wmv|WAV|* .wav|MKV|*.mkv “ })

First, declare the openFileDialog for opening the dialogbox for selection files from the drive. and Boolean method of Multiselect method to true and another ValidateNames to true and add the different filters which can be picked by the media player.

Then I set a condition to check if the:

openFile.ShowDialog() == DialogResult.OK so save the name of the in a list:

List<MediaFile> files = new List<MediaFile>();

foreach (string filename in openFile.FileNames)

{

FileInfo f1 = new FileInfo(filename);

files.Add(new MediaFile() { FileName = Path.GetFileNameWithoutExtension(f1.FullName), Path = f1.FullName });

}

listBox1.DataSource = files; listBox1.ValueMember = “Path”; listBox1.DisplayMember = “FileName”;

When you double click on the ListBox;

MediaFile file = listBox1.SelectedItem as MediaFile;

if (file != null) { axWindowsMediaPlayer1.URL = file.Path; axWindowsMediaPlayer1.Ctlcontrols.play(); axWindowsMediaPlayer1.Ctlcontrols.next(); axWindowsMediaPlayer1.Ctlcontrols.previous(); }

The above code checks if the listbox is empty and prompts the user to add a music format to the Listbox which will be loaded.

Conclusion

The project is just one of many things C# and .NET framework can do. To get the full source code you can clone and download it from my Github repository.

You can check out my Portfolio and lets connect on LinkedIn.

A quote by famous programmers: “Talk is cheap. Show me the code.” ― Linus Torvalds

--

--