C# Tutorial – Make a Video Player App in Windows Form Application

In this tutorial we will make our own media player app. We made a tutorial on how to load all of the pictures from a folder and turn it into a slide show before, this time I wanted to make an app that loads all of the video files as MP4, MKV and WEBM to a list and plays them automatically one by one. I found this app very helpful when I was backing up my phone. it had all of the videos and the pictures together so I had a way to see all of the video files in the folder without sort of going through the thousands of files.  This application uses the Windows Media Player controls, .Net Framework with C# to work. This tutorial will show you how to make this app step by step and hopefully you can make your own version of this media player.

Lesson objectives –

  1. How to make a media player with a playlist using windows form and C# programming
  2. Import media player controls to the tool box
  3. Use list box, labels, menu strips and media player to make this app
  4. Use System.IO to read external files, get paths, get directory
  5. Use Lists to filter through media files only and store them in the list
  6. Use the list event to change the videos playing on the media player

Video

Full Source Code –

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.IO;


namespace Folder_Media_Player_MOO_ICT
{
    public partial class Form1 : Form
    {

        List<string> filteredFiles = new List<string>();
        FolderBrowserDialog browser = new FolderBrowserDialog();
        int currentFile = 0;


        public Form1()
        {
            InitializeComponent();
        }

        private void LoadFolderEvent(object sender, EventArgs e)
        {
            VideoPlayer.Ctlcontrols.stop();

            if (filteredFiles.Count > 1)
            {
                filteredFiles.Clear();
                filteredFiles = null;

                PlayList.Items.Clear();

                currentFile = 0;
            }

            DialogResult result = browser.ShowDialog();

            if (result == DialogResult.OK)
            {
                filteredFiles = Directory.GetFiles(browser.SelectedPath, "*.*").Where(file => file.ToLower().EndsWith("webm") || file.ToLower().EndsWith("mp4") || file.ToLower().EndsWith("wmv") || file.ToLower().EndsWith("mkv") || file.ToLower().EndsWith("avi")).ToList();

                LoadPlayList();
            }


        }

        private void ShowAboutEvent(object sender, EventArgs e)
        {
            MessageBox.Show("This app is made by MOO ICT" + Environment.NewLine + "Hope you enjoyed the simple media player" + Environment.NewLine + "Click on Open Folder Button to load the video folder to the app and it will start auto playing" + Environment.NewLine + "Enjoy", "MOO SAYS: ");
        }

        private void MediaPlayerStateChangeEvent(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
        {
            if (e.newState == 0)
            {
                // undefined loaded

                lblDuration.Text = "Media Player is Ready to be loaded";
            }
            else if (e.newState == 1)
            {
                // if the file is stopped 

                lblDuration.Text = "Media Player stopped";
            }
            else if (e.newState == 3)
            {
                // if the file is playing 

                lblDuration.Text = "Duration: " + VideoPlayer.currentMedia.durationString;
            }
            else if (e.newState == 8)
            {
                // media has ended here

                if (currentFile >= filteredFiles.Count - 1)
                {
                    currentFile = 0;
                }
                else
                {
                    currentFile += 1;
                }

                PlayList.SelectedIndex = currentFile;

                ShowFileName(fileName);

            }
            else if (e.newState == 9)
            {
                // if the media player is loading new video
                lblDuration.Text = "Loading new video";
            }
            else if (e.newState == 10)
            {
                // media is ready to play again
                timer1.Start();

            }

        }

        private void PlayListChanged(object sender, EventArgs e)
        {
            currentFile = PlayList.SelectedIndex;
            PlayFile(PlayList.SelectedItem.ToString());
            ShowFileName(fileName);
        }

        private void TimerEvent(object sender, EventArgs e)
        {
            VideoPlayer.Ctlcontrols.play();
            timer1.Stop();
        }

        private void LoadPlayList()
        {
            VideoPlayer.currentPlaylist = VideoPlayer.newPlaylist("PlayList", "");

            foreach (string videos in filteredFiles)
            {
                VideoPlayer.currentPlaylist.appendItem(VideoPlayer.newMedia(videos));
                PlayList.Items.Add(videos);
            }

            if (filteredFiles.Count > 0)
            {
                fileName.Text = "Files Found " + filteredFiles.Count;

                PlayList.SelectedIndex = currentFile;

                PlayFile(PlayList.SelectedItem.ToString());
            }
            else
            {
                MessageBox.Show("No Video Files Found in this folder");
            }
        }

        private void PlayFile(string url)
        {
            VideoPlayer.URL = url;
        }

        private void ShowFileName(Label name)
        {
            string file = Path.GetFileName(PlayList.SelectedItem.ToString());
            name.Text = "Currently Playing: " + file;
        }
    }
}

 




One response to “C# Tutorial – Make a Video Player App in Windows Form Application”

  1. Wester says:

    Nice video thanks. How we can make the playlist auto played