A simple media player program in C# .NET 4.0

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.

using System; 
using System.Collections.Generic;

using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace mediaplayer
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
        public MainWindow()

        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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s