Creating Your Own Custom WPF Media Player - Exploring the Hard Disk
Auto Beauty Business Culture Dieting DIY Events Fashion Finance Food Freelancing Gardening Health Hobbies Home Internet Jobs Law Local Media Men's Health Mobile Nutrition Parenting Pets Pregnancy Products Psychology Real Estate Relationships Science Seniors Sports Technology Travel Wellness Women's Health

Creating Your Own Custom WPF Media Player - Exploring the Hard Disk

How to create the play/pause and stop buttons for a custom made WPF media player, and how to create HDD explore dialogs and open files on the hard disk

Welcome back to the custom WPF media player tutorial. This time, we shall handle the play/pause and stop buttons and, of course, choosing which file we want to play.

Our goal

We want our media player to open a specific file and play it. For this we need event handlers for play, pause, stop buttons as well as a browse hard disk function.

In the code behind MainWindow, add the following global variables:

public static string sursa;

public static bool areThereAnyItemsInThePlayList = false;

bool is playing=false;

The static modifier in front of a variable/method makes that variable/method invisible when you create an instance of the object containing them. However, these variables will now act as global variables inside the namespace (and outside for that matter), which means we can use them in other classes as well.

Now, go to the “Form1”(the other window created in the first part) tab and double click the “Browse hard disk button”. This will open up the click event handler for that button. We are not yet interested in it, but we needed to get to code view.

In Form1.cs add the following global variables:

OpenFileDialog fDialog = new OpenFileDialog();

Which is a file dialog window that will allow us to explore the hard disk and chose which file we want to open.

public static string BROWSE_result;

In the click event handler for the button we talked about earlier, add these lines of code

fDialog.Title = "Open: ";

fDialog.Filter = "AVI files|*.avi|mp3 Files|*.mp3|All files (*.*)|*.*";

fDialog.InitialDirectory = @"C:\";

if (fDialog.ShowDialog() == DialogResult.OK)

{

BROWSE_result = fDialog.FileName.ToString();

textBox1.Text = fDialog.FileName.ToString();

MainWindow.sursa = BROWSE_result;

}

fDialog.ShowHelp = true;

Basically, when the button is clicked, we open the hard disk explorer dialog. The filter is self-explanatory I hope, just like the initial directory. The “if” over there basically checks if the Open button in the dialog was clicked, and if a valid file was opened. After that, the address of the file is converted to string and retained in “BROWSE_result”. The debugging textbox will also display the address of the file. After that, the public static string sursa from MainWindow is given the content of BROWSE_result.

Now, double click on the other button in “Form1” and add these lines

private void PLAY_Click_1(object sender, EventArgs e)

{

MainWindow.areThereAnyItemsInThePlayList = true;

this.Close();

}

Basically, this is everything there is to do about form1. We won’t be editing the code behind this window ever again.

Now, go back to MainWindow, and double click the PLAY button; in the event handler, add the following lines of code

private void Button_play_pause_Click(object sender, RoutedEventArgs e)

{

if (areThereAnyItemsInThePlayList)

{

try

{

Uri media_element_source = new Uri(sursa);

media_Element.Source = media_element_source;

if (isPLAYING == false)

{

media_Element.Play();

Button_play_pause.Content = "PAUSE";

isPLAYING = true;

}

else if (isPLAYING == true)

{

media_Element.Pause();

Button_play_pause.Content = "PLAY";

isPLAYING = false;

}

}

catch (ArgumentNullException)

{

Form1 expp = new Form1();

expp.Show();

}

}

else

{

Form1 expp = new Form1();

expp.Show();

}

}

As I mentioned in an earlier part of this tutorial, the source property for media element class in an object of type Uri. Uri also have one constructor with one string parameter, and we will be using that one to convert the string “sursa” containing the browse result from the other window to Uri. After that, we simply set media_element.surce to the Uri we just created. The Try-Catch code is there to prevent any errors from occurring. In the case the user had not opened any files and decides to press the Play button, the ArgumentNullException will be triggered when we want to create the Uri object. In that case, an instance of Form1 will be opened so that the sloppy user can open a file on the hard disk. Of course, you may ask yourself why is there the try-catch block when the If statement on top of them already prevents that: the answer is simple: sometimes, data can be lost or variable values corrupted due to various reasons, so it is best to have as many safeguards as possible. I made it so that the play and pause buttons are all in one. You can, of course, create different buttons if you wish.

Another question you might have is why I transfer the content from BROWSE_result from Form1 to sursa in MainWindow, and not do the conversion directly from BROWSE_result.

I transfer it because I want to make sure the data is still there and doesn’t get garbage collected after I close the Form1 window. Of course, there are other ways of obtaining the same result, so feel free to code the way you want.

The stop button looks like this:

private void Button_stop_Click(object sender, RoutedEventArgs e)

{

media_Element.Stop();

isPLAYING = false;

Button_play_pause.Content = "PLAY";

}

 

Shopping online? Find the latest coupon codes for programming tools brands and score big discounts on your favorite brands. Shop through our partner network for the best discounts on popular programming tools stores with exclusive discounts, site-wide promo codes, and single-use codes.
Need an answer?
Get insightful answers from community-recommended
experts
in Computer Programming & Languages on Knoji.
Would you recommend this author as an expert in Computer Programming & Languages?
You have 0 recommendations remaining to grant today.
Comments (1)

It is good to know about the hard disk drive internal parts. Thanks for sharing this informative article, voted and shared.

ARTICLE DETAILS
RELATED ARTICLES
RELATED CATEGORIES
ARTICLE KEYWORDS