This is a program sample that you should go through at the intermediate level of your knowledge about the .Net platform by Microsoft. The entire program is written in C#, you may also use VB.NET if you are familiar with it. The logic behind the program is same, just coding syntax will vary. Well, all the best! let’s start with a quick glance at our objective :
i. We shall construct a media player.
ii. It shall be capable of playing media files of any kind, say a music file of .mp3 or .ogg format, a picture of .jpg or .png format as well as a video file of .wmv or .avi format.
iii. We shall be able to add files, play them, pause them and stop them.
For, our purpose we will use WPF(Windows Presentation Foundation) standard form in Visual Studio 2010 edition.
1. Create New Project.
2. Choose WPF, give a project name and add it. A workspace is created with a default Form loaded with the XAML code engraved in it.
3. Open toolbox and choose rather drag and drop a media element on the workspace.
4. Add 4 buttons name them as ‘PLAY’,’PAUSE’,STOP’,’ADD MEDIA’ respectively and a textbox to display the filename being played. A screenshot is given below:
|Screenshot of basic layout of WPF form.|
5. Double-click on ‘ADD MEDIA’ button and it shall take you to the MainWindow.xaml.cs file where we write our code. Now, a default method is called as we are double clicking the ‘button4’(just an index), that is, button4_click. Now I shall write the entire code.
/// Interaction logic for MainWindow.xaml
public partial class MainWindow : Window
private void button4_Click(object sender, RoutedEventArgs e)
OpenFileDialog ofd = new OpenFileDialog();
ofd.AddExtension = true;
ofd.DefaultExt = "*.*";
ofd.Filter = "Media(*.*)|*.*";
mediaElement1.MediaOpened += new RoutedEventHandler(mediaElement1_MediaOpened);
mediaElement1.Source = new Uri(ofd.FileName);
private void button1_Click(object sender, RoutedEventArgs e)
private void button3_Click(object sender, RoutedEventArgs e)
private void button2_Click(object sender, RoutedEventArgs e)
void mediaElement1_MediaOpened(object sender, RoutedEventArgs e)
label1.Content = mediaElement1.Source.ToString();
6. Press CTRL+F5 for the code to run. And there you go! Click on ‘Add Media’ button and choose media and watch it play!
Enjoy! Have Fun!
This link might be useful for you: http://www.youtube.com/watch?v=f_bBWHCoKpk