Creating Your Own Custom WPF Media Player - Playlists
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 - Playlists

How to create a playlist in a custom made WPF media player using C# 4

Hello and welcome back to the tutorial. From now on, all new articles part of the tutorial will simply be added utilities to the player. They will not be required to run the player in a basic mode, and it is advised to save a copy of the initial player somewhere safe.

Today we shall handle playlists. There are several ways of creating a playlists. One of them would be using a vector (1-sized arry); LINQ can also be used to create a database which would act as a playlist. You can also create a separate class and make it act like a structure (double linked circular list). Anyway, we shall take the vector method, which is easiest to implement and fastest as far as performance is concerned.

First of all, we need to add another class to this project. Use the project tab to do so. Name the new class “utilities” and let it be in the default namespace (which is WpfApplication1 or something similar). In this class, create a public static int nr_rezultate which is a variable used to count how many items are in the playlist. We do this to circumvent the garbage collector which would otherwise delete this variable when we are done with adding items in the playlist. So far, there is only one vairable in this class, but we shall use it to add utilities as we need them.

Now go to the Forms1.cs file. You will want to make the public static string BROWSE_result into a vector. Declare it in the likes of this:

public static string[] BROWSE_result=new String[1];

Why we allocate memory? Because we need it to circumvent the garbage collector. Had we allocated the memory somewhere else, the information in it would be lost when the program exists that region of text, so we keep this variable as global.

Now go to the Browse button click event handler and add the following lines

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

{

WpfApplication1.utilities.nr_rezultate++;

if (areThereAnyItemsAdded)

{

Array.Resize(ref BROWSE_result, BROWSE_result.Length + 1);

BROWSE_result[WpfApplication1.utilities.nr_rezultate - 1] = fDialog.FileName.ToString();

}

else

{

BROWSE_result[0] = fDialog.FileName.ToString();

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

areThereAnyItemsAdded = true;

}

Don’t forget to add the bool areThereAnyItemsAdded=false as variable in the class.

So basically, we treat 2 cases here. If there were no items in the playlist previously added, then the BROWSE_result arry would only have 1 element (index is 0). So we simply keep the address of the file chosen in BROWSE_result[0] (that first element); If there already are items in the playlist, we increase the size of BROWSE_result by 1. After that, we add the newest entry in the playlist to the last address in BROWSE_result.

Now, go to MainWindow.xaml.cs file.

In the event handlers for FORWARDS and BACKWARDS buttons (remember them from part 1?) add these lines of code

private void Forward_Click(object sender, RoutedEventArgs e)

{

if (i < WpfApplication1.utilities.nr_rezultate)

{

i++;

}

else if (i >= WpfApplication1.utilities.nr_rezultate)

{

i = 0;

}

else if (i < 0)

{

i = 0;

}

}

private void backward_Click(object sender, RoutedEventArgs e)

{

if (i <= WpfApplication1.utilities.nr_rezultate)

{

i--;

}

else if (i < 0)

{ i = 0; }

}

Public static int i is an index variable used to signal the position in the playlist.

In the PLAY_PAUSE click event, alter these lines of code

Uri media_element_source = new Uri(Form1.BROWSE_result[i]);

media_Element.Source = media_element_source;

Don’t forget to delete some of the now obsolete variables. You should also add another Catch statement to the Try block to handle any possible IndexOutOfBounds exception. Feel free to re-build the application and enjoy the playlist :).

 

Consumer tip: Get discounts on leading programming tools brands by exploring the latest discount codes for programming tools. Utilize community-sourced coupons, promo codes, and in-store offers using our partner websites.
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 (0)
ARTICLE DETAILS
RELATED ARTICLES
RELATED CATEGORIES
ARTICLE KEYWORDS