Skip to main content

How to Create Video Player using WinForm C#

Here, I will explain how to store audio and video files in, and how to restore them from, a local database then how to play them in the Windows Media Player. I used a COM component, in other words the built-in media player in Windows. I will explain the following points:

Step 1: Go to "New Project" -> "Windows Forms application" then click on "Ok".

Step 2: Add two Buttons, one ComboBox and an OpenFileDialouge from the Toolbox.

Step 3: Now add a COM Component to the form.

Open the ToolBox then Right-click then choose "Items" ->  "COM components" then select "Windows Media Player" then click "Ok". Next it will add something from the ToolBox then drag it from the Toolbox to the form.

Our Form Look like this:

  • axWindowsMediaPlayer1 control used to play video
  • timerCheckState control used to check video in folder every 5000 ms
  • txtVideoChanged control used to display log checking new video in folder



Code:
public partial class Player : Form
    {
        private string path = @"D:\VAR\001.mp4";
        private List<string> videosFiles = new List<string>();
        public Player()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string[] files = Directory.GetFiles(@"D:\VAR","*.mp4");
            videosFiles = files.ToList();
            PlayVideoByPlaylist(CreatePlayList("myPlaylist"));
            timerCheckState.Start();
        }
        void PlayVideoByName(string path)
        {
            ResetVideo();
            axWindowsMediaPlayer1.URL = path;
            axWindowsMediaPlayer1.settings.autoStart = true;
            axWindowsMediaPlayer1.uiMode = "None";
        }
        void PlayVideoByPlaylist(WMPLib.IWMPPlaylist playlist)
        {
            ResetVideo();
            axWindowsMediaPlayer1.currentPlaylist = playlist;
            //axWindowsMediaPlayer1.uiMode = "None";
            axWindowsMediaPlayer1.Ctlcontrols.play();
        }
        void ResetVideo()
        {
            axWindowsMediaPlayer1.Ctlcontrols.stop();
        }
        WMPLib.IWMPPlaylist CreatePlayList(string name)
        {
            WMPLib.IWMPPlaylist playlist = axWindowsMediaPlayer1.playlistCollection.newPlaylist(name);
            WMPLib.IWMPMedia media;
            foreach (var item in videosFiles)
            {
                media = axWindowsMediaPlayer1.newMedia(item);
                playlist.appendItem(media);
            }
            return playlist;
        }
        private void timerCheckState_Tick(object sender, EventArgs e)
        {
            List<string> files = Directory.GetFiles(@"D:\VAR", "*.mp4").ToList();
            var firstNotSecond = videosFiles.Except(files).ToList();
            var secondNotFirst = files.Except(videosFiles).ToList();
            var dd = axWindowsMediaPlayer1.currentPlaylist.count;
            if (!firstNotSecond.Any() && !secondNotFirst.Any())
            {
                txtVideoChanged.Text += "No New Video Found!" + Environment.NewLine;
            }
            else
            {
                txtVideoChanged.Text += "New Video Found!" + Environment.NewLine;
                WMPLib.IWMPMedia media = null;
                WMPLib.IWMPMedia removeMedia = null;
                foreach (var item in firstNotSecond)
                {
                    videosFiles.Remove(item);
                    for (int i = 0; i < axWindowsMediaPlayer1.currentPlaylist.count-1; i++)
                    {
                        string mediaName = axWindowsMediaPlayer1.currentPlaylist.Item[i].sourceURL;
                        if (mediaName == item)
                        {
                            WMPLib.IWMPMedia3 rmmd = (WMPLib.IWMPMedia3)axWindowsMediaPlayer1.mediaCollection.getAll().get_Item(i);
                            axWindowsMediaPlayer1.mediaCollection.remove(rmmd, true);
                            axWindowsMediaPlayer1.currentPlaylist.removeItem(axWindowsMediaPlayer1.currentPlaylist.Item[i]);
                            txtVideoChanged.Text += $"{item} Removed!" + Environment.NewLine;
                        }
                    }
                }
                foreach (var item in secondNotFirst)
                {
                    media = axWindowsMediaPlayer1.newMedia(item);
                    videosFiles.Add(item);
                }
                if (media !=null)
                {
                    axWindowsMediaPlayer1.currentPlaylist.appendItem(media);
                    txtVideoChanged.Text += "New Video Added!" + Environment.NewLine;
                }
            }
        }
    }

For this example my video directory is in @"D:\VAR";
End, any question please comment blow;

Comments