How to create a Media player in C#

Now we are going to learn about how to create a media player in C#.
  • 9693

Introduction

Media player is very simple and easy in C#. First you have to open a new WPF window form, drag and drop media elements and four button use for PLAY, PAUSE, STOP, ADD_MEDIA from toolbox. And write given code.

Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Forms;
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 WpfApplication1
{
    public partial class MainWindow :
Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
        }
        private void ADDMEDIA_Click(object sender, RoutedEventArgs e)
        {            OpenFileDialog open = new OpenFileDialog();
           open.AddExtension = true;
            open.DefaultExt = "*.*";
             open.Filter = "Media(*.*)|*.*";
             open.ShowDialog();           
            mediaElement1.MediaOpened += new RoutedEventHandler(mediaElement1_MediaOpened);
            mediaElement1.Source = new Uri(open.FileName);
        }
        private void Play_Click(object sender, RoutedEventArgs e)
        {
            mediaElement1.Play();
        }
        private void PAUSE_Click(object sender, RoutedEventArgs e)
        {
            mediaElement1.Pause();
        }
        private void STOP_Click(object sender, RoutedEventArgs e)
        {
            mediaElement1.Stop();
        }
        void mediaElement1_MediaOpened(object sender, RoutedEventArgs e)
        {          
          
//label1.Content = mediaElement1.Source.ToString();
        }    }}

 Output

media.jpg

© 2013 dotNetheaven. All rights reserved.