Simple video Player

In this tutorial we’re going to see how to create a very simple video player in C# with Visual Studio. Here is what we are going to make:

Requirements

What are we going to do

We’re going to make a software that plays video files from the computer or from internet, to do that we’re going to use “LibVLCSharp” that is a C# wrapper of the VideoLAN’s LibVLC Library.
Some features that we’re going to make are:

What we need to install

You need to install Visual Studio 2017(or later), and create a new “App Windows Form” project.
Then through nuget you need to install:

Once you’ve installed all this you can go on.

The first form

By opening a new project, visual studio will create a windows form.
In this one drag a “videoView” component from the toolbox, you can find it inside the “LibVLCSharp.WinForms” group, then drag a “menuStrip” component.
Set a size that you like for the videoView, then from the “Proprieties” menu set it anchor from each side, this will autoimaticaly resize the videoView based on the form size.
From the menuStrip add a File menu, then inside it add these options:

Then add a new menu item, call it “Tools” and inside it add:

Now you must have something like this:
Responsive image

Adding Code to form1

Now right click on the Form1 and choose “view code”.
At the top add:

1using LibVLCSharp.Shared;

Inside the form1 class add these variables:

 1//VLC stuff
 2public LibVLC _libVLC;
 3public MediaPlayer _mp;
 4public Media media;
 5
 6public bool isFullscreen = false;
 7public bool isPlaying = false;
 8public Size oldVideoSize;
 9public Size oldFormSize;
10public Point oldVideoLocation;

I will explain these variables later, when we’ll use them.
Inside the “Form1()” function, just after “InitializeComponent();” we need to add:

 1Core.Initialize();
 2this.KeyPreview = true;
 3this.KeyDown += new KeyEventHandler(ShortcutEvent);
 4oldVideoSize = videoView1.Size;
 5oldFormSize = this.Size;
 6oldVideoLocation = videoView1.Location;
 7//VLC stuff
 8_libVLC = new LibVLC();
 9_mp = new MediaPlayer(_libVLC);
10videoView1.MediaPlayer = _mp;

Core.Initialize(); will initialize the VLC library.
this.KeyPreview = true; this and this.KeyDown += new KeyEventHandler(ShortcutEvent); allow us to make the shortcuts.

oldVideoSize = videoView1.Size;
oldFormSize = this.Size;
oldVideoLocation = videoView1.Location;
These 3 variables we’ll store the size of the form1 and the videoView1 components, we’ll use them when we’ll comeback to the windowed mode from the fullscreen.

_libVLC = new LibVLC();
_mp = new MediaPlayer(_libVLC);
videoView1.MediaPlayer = _mp;
These 3 lines are necessary to setup the VLC library, the last one connect the videoView1 mediaPlayer to the mediaplayer of the library.

Handle the shortcuts

Now we need to create a function for the shortcuts to control the player. Through them we’ll be able to play and pause the video and also to skip forward and backwards.
Now let’s go into the code side:

 1public void ShortcutEvent(object sender, KeyEventArgs e)
 2{
 3    if (e.KeyCode == Keys.Escape && isFullscreen) // from fullscreen to window
 4    {
 5        this.FormBorderStyle = FormBorderStyle.Sizable; // change form style
 6        this.WindowState = FormWindowState.Normal; // back to normal size
 7        this.Size = oldFormSize;
 8        menuStrip1.Visible = true; // the return of the menu strip 
 9        videoView1.Size = oldVideoSize; // make video the same size as the form
10        videoView1.Location = oldVideoLocation; // remove the offset
11        isFullscreen = false;
12    }
13
14    if (isPlaying) // while the video is playing
15    {
16        if (e.KeyCode == Keys.Space) // Pause and Play
17        {
18            if (_mp.State == VLCState.Playing) // if is playing
19            {
20                _mp.Pause(); // pause
21            }
22            else // it's not playing?
23            {
24                _mp.Play(); // play
25            }
26        }
27
28        if (e.KeyCode == Keys.J) // skip 1% backwards
29        {
30            _mp.Position -= 0.01f;
31        }
32        if (e.KeyCode == Keys.L) // skip 1% forwards
33        {
34            _mp.Position += 0.01f;
35        }
36    }
37}

public void ShortcutEvent(object sender, KeyEventArgs e){...} This is the function that handles the shortcuts, we called it inside the Form1() with the function this.KeyDown += new KeyEventHandler(ShortcutEvent);
if (e.KeyCode == Keys.Escape && isFullscreen){...} this condition will handle what happen when we’re coming back from the full screen view. It restore the old size of the VideoView and the from1.
if (isPlaying){...} this will handle the shortcuts of the player while it’s playing a video.

Go Fullscreen

Let’s add the a function for every item of the menu strip, from the designer just double click in “Opens file…”, “Open URL”, “Exit” and “go Fullscreen”, this will automatically create a function for each item.
Inside the “go fullscreen” function add:

1menuStrip1.Visible = false; // goodbye menu strip
2videoView1.Size = this.Size; // make video the same size as the form
3videoView1.Location = new Point(0, 0); // remove the offset
4this.FormBorderStyle = FormBorderStyle.None; // change form style
5this.WindowState = FormWindowState.Maximized;
6isFullscreen = true;

With this function we want to make the player fullscreen, to do that we change the form border size so it won’t have any control buttons, we remove the start menu and we’ll set the windows state to maximized, so it will cover all the screen. We set an new location of the videoView because when the program is windowed is located under the menu strip, if we don’t change it and we go in fullscreen mode it will leave a little gap above the video.

Exit

To close the program we just need to call:

1Application.Exit();

Open URL

This function will open a custom file dialog, we’ll make it later, for now just write:

1Form2 url_ofd = new Form2();
2url_ofd.Show();

Open File

This must show a dialog that let us choose a file from our pc.

1OpenFileDialog ofd = new OpenFileDialog();
2if(ofd.ShowDialog() == DialogResult.OK)
3{
4    PlayFile(ofd.FileName);
5}

We are going to create the PlayFile function later.

PlayFile Function

Now we need to create a function that will play the video from a file:

1public void PlayFile(string file)
2{
3    _mp.Play(new Media(_libVLC, file));
4    isPlaying = true;
5}

PlayURLFile Function

This function will allow us to play a video from a file that is not inside our computer:

1public void PlayURLFile(string file)
2{
3    _mp.Play(new Media(_libVLC, new Uri(file)));
4    isPlaying = true;
5}

The second form

Now we need to add a new windows form.
This second form is the one we called for open a video file from the URL.
From the design Add a text box and a button, if you want you can add a label.
Select the form and from the proprieties change the “borderStyle” to “fixed 3d” so it cannot be resized.

It should look something like this:
Responsive image

Now double click on the button, and inside its function paste:

1(System.Windows.Forms.Application.OpenForms["Form1"] as Form1).PlayURLFile(textBox1.Text);

This will call the function PlayURLFile of the form1 without create a new instance.

Play with the… player!

This simple player is now complete.
If you have any troubles you can look at the source code inside the next section, if you copy and paste it just remember to change the namespacešŸ˜„

Full Source Code

Here you’ll find the full source of the player.

Form1.Designer.cs

  1namespace SimpleVideoPlayer
  2{
  3    partial class Form1
  4    {
  5
  6        private System.ComponentModel.IContainer components = null;
  7
  8        protected override void Dispose(bool disposing)
  9        {
 10            if (disposing && (components != null))
 11            {
 12                components.Dispose();
 13            }
 14            base.Dispose(disposing);
 15        }
 16
 17        private void InitializeComponent()
 18        {
 19            this.videoView1 = new LibVLCSharp.WinForms.VideoView();
 20            this.menuStrip1 = new System.Windows.Forms.MenuStrip();
 21            this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
 22            this.openToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
 23            this.openURLToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
 24            this.exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
 25            this.toolsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
 26            this.goFullscreenToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
 27            ((System.ComponentModel.ISupportInitialize)(this.videoView1)).BeginInit();
 28            this.menuStrip1.SuspendLayout();
 29            this.SuspendLayout();
 30            // 
 31            // videoView1
 32            // 
 33            this.videoView1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
 34            | System.Windows.Forms.AnchorStyles.Left) 
 35            | System.Windows.Forms.AnchorStyles.Right)));
 36            this.videoView1.BackColor = System.Drawing.Color.Black;
 37            this.videoView1.Location = new System.Drawing.Point(0, 27);
 38            this.videoView1.MediaPlayer = null;
 39            this.videoView1.Name = "videoView1";
 40            this.videoView1.Size = new System.Drawing.Size(800, 444);
 41            this.videoView1.TabIndex = 0;
 42            this.videoView1.Text = "videoView1";
 43            // 
 44            // menuStrip1
 45            // 
 46            this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
 47            this.fileToolStripMenuItem,
 48            this.toolsToolStripMenuItem});
 49            this.menuStrip1.Location = new System.Drawing.Point(0, 0);
 50            this.menuStrip1.Name = "menuStrip1";
 51            this.menuStrip1.Size = new System.Drawing.Size(800, 24);
 52            this.menuStrip1.TabIndex = 1;
 53            this.menuStrip1.Text = "menuStrip1";
 54            // 
 55            // fileToolStripMenuItem
 56            // 
 57            this.fileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
 58            this.openToolStripMenuItem,
 59            this.openURLToolStripMenuItem,
 60            this.exitToolStripMenuItem});
 61            this.fileToolStripMenuItem.Name = "fileToolStripMenuItem";
 62            this.fileToolStripMenuItem.Size = new System.Drawing.Size(37, 20);
 63            this.fileToolStripMenuItem.Text = "File";
 64            // 
 65            // openToolStripMenuItem
 66            // 
 67            this.openToolStripMenuItem.Name = "openToolStripMenuItem";
 68            this.openToolStripMenuItem.Size = new System.Drawing.Size(136, 22);
 69            this.openToolStripMenuItem.Text = "Open File...";
 70            this.openToolStripMenuItem.Click += new System.EventHandler(this.openToolStripMenuItem_Click);
 71            // 
 72            // openURLToolStripMenuItem
 73            // 
 74            this.openURLToolStripMenuItem.Name = "openURLToolStripMenuItem";
 75            this.openURLToolStripMenuItem.Size = new System.Drawing.Size(136, 22);
 76            this.openURLToolStripMenuItem.Text = "Open URL...";
 77            this.openURLToolStripMenuItem.Click += new System.EventHandler(this.openURLToolStripMenuItem_Click);
 78            // 
 79            // exitToolStripMenuItem
 80            // 
 81            this.exitToolStripMenuItem.Name = "exitToolStripMenuItem";
 82            this.exitToolStripMenuItem.Size = new System.Drawing.Size(136, 22);
 83            this.exitToolStripMenuItem.Text = "Exit";
 84            this.exitToolStripMenuItem.Click += new System.EventHandler(this.exitToolStripMenuItem_Click);
 85            // 
 86            // toolsToolStripMenuItem
 87            // 
 88            this.toolsToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
 89            this.goFullscreenToolStripMenuItem});
 90            this.toolsToolStripMenuItem.Name = "toolsToolStripMenuItem";
 91            this.toolsToolStripMenuItem.Size = new System.Drawing.Size(46, 20);
 92            this.toolsToolStripMenuItem.Text = "Tools";
 93            // 
 94            // goFullscreenToolStripMenuItem
 95            // 
 96            this.goFullscreenToolStripMenuItem.Name = "goFullscreenToolStripMenuItem";
 97            this.goFullscreenToolStripMenuItem.Size = new System.Drawing.Size(143, 22);
 98            this.goFullscreenToolStripMenuItem.Text = "Go fullscreen";
 99            this.goFullscreenToolStripMenuItem.Click += new System.EventHandler(this.goFullscreenToolStripMenuItem_Click);
100            // 
101            // Form1
102            // 
103            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
104            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
105            this.ClientSize = new System.Drawing.Size(800, 464);
106            this.Controls.Add(this.videoView1);
107            this.Controls.Add(this.menuStrip1);
108            this.MainMenuStrip = this.menuStrip1;
109            this.Name = "Form1";
110            this.Text = "Form1";
111            ((System.ComponentModel.ISupportInitialize)(this.videoView1)).EndInit();
112            this.menuStrip1.ResumeLayout(false);
113            this.menuStrip1.PerformLayout();
114            this.ResumeLayout(false);
115            this.PerformLayout();
116
117        }
118
119        #endregion
120
121        private LibVLCSharp.WinForms.VideoView videoView1;
122        private System.Windows.Forms.MenuStrip menuStrip1;
123        private System.Windows.Forms.ToolStripMenuItem fileToolStripMenuItem;
124        private System.Windows.Forms.ToolStripMenuItem openToolStripMenuItem;
125        private System.Windows.Forms.ToolStripMenuItem openURLToolStripMenuItem;
126        private System.Windows.Forms.ToolStripMenuItem exitToolStripMenuItem;
127        private System.Windows.Forms.ToolStripMenuItem toolsToolStripMenuItem;
128        private System.Windows.Forms.ToolStripMenuItem goFullscreenToolStripMenuItem;
129    }
130}

Form1.cs

  1using System;
  2using System.Collections.Generic;
  3using System.ComponentModel;
  4using System.Data;
  5using System.Drawing;
  6using System.Linq;
  7using System.Text;
  8using System.Threading.Tasks;
  9using System.Windows.Forms;
 10using LibVLCSharp.Shared;
 11
 12namespace SimpleVideoPlayer
 13{
 14    public partial class Form1 : Form
 15    {
 16        //VLC stuff
 17        public LibVLC _libVLC;
 18        public MediaPlayer _mp;
 19        public Media media;
 20
 21        public bool isFullscreen = false;
 22        public bool isPlaying = false;
 23        public Size oldVideoSize;
 24        public Size oldFormSize;
 25        public Point oldVideoLocation;
 26
 27        public Form1()
 28        {
 29            InitializeComponent();
 30            Core.Initialize();
 31            this.KeyPreview = true;
 32            this.KeyDown += new KeyEventHandler(ShortcutEvent);
 33            oldVideoSize = videoView1.Size;
 34            oldFormSize = this.Size;
 35            oldVideoLocation = videoView1.Location;
 36            //VLC stuff
 37            _libVLC = new LibVLC();
 38            _mp = new MediaPlayer(_libVLC);
 39            videoView1.MediaPlayer = _mp;
 40        }
 41
 42        public void ShortcutEvent(object sender, KeyEventArgs e)
 43        {
 44            if (e.KeyCode == Keys.Escape && isFullscreen) // from fullscreen to window
 45            {
 46                this.FormBorderStyle = FormBorderStyle.Sizable; // change form style
 47                this.WindowState = FormWindowState.Normal; // back to normal size
 48                this.Size = oldFormSize;
 49                menuStrip1.Visible = true; // the return of the menu strip 
 50                videoView1.Size = oldVideoSize; // make video the same size as the form
 51                videoView1.Location = oldVideoLocation; // remove the offset
 52                isFullscreen = false;
 53            }
 54
 55            if (isPlaying) // while the video is playing
 56            {
 57                if (e.KeyCode == Keys.Space) // Pause and Play
 58                {
 59                    if (_mp.State == VLCState.Playing)
 60                    {
 61                        _mp.Pause();
 62                    }
 63                    else
 64                    {
 65                        _mp.Play();
 66                    }
 67                }
 68
 69                if (e.KeyCode == Keys.J) // skip 1% backwards
 70                {
 71                    _mp.Position -= 0.01f;
 72                }
 73                if (e.KeyCode == Keys.L) // skip 1% forwards
 74                {
 75                    _mp.Position += 0.01f;
 76                }
 77            }
 78        }
 79
 80        private void goFullscreenToolStripMenuItem_Click(object sender, EventArgs e)
 81        {
 82            menuStrip1.Visible = false; // goodbye menu strip
 83            videoView1.Size = this.Size; // make video the same size as the form
 84            videoView1.Location = new Point(0, 0); // remove the offset
 85            this.FormBorderStyle = FormBorderStyle.None; // cheange form style
 86            this.WindowState = FormWindowState.Maximized;
 87            isFullscreen = true;
 88
 89        }
 90
 91        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
 92        {
 93            Application.Exit();
 94        }
 95
 96        private void openURLToolStripMenuItem_Click(object sender, EventArgs e)
 97        {
 98            Form2 url_ofd = new Form2();
 99            url_ofd.Show();
100        }
101
102        private void openToolStripMenuItem_Click(object sender, EventArgs e)
103        {
104            OpenFileDialog ofd = new OpenFileDialog();
105            if(ofd.ShowDialog() == DialogResult.OK)
106            {
107                PlayFile(ofd.FileName);
108            }
109        }
110
111        public void PlayFile(string file)
112        {
113            _mp.Play(new Media(_libVLC, file));
114            isPlaying = true;
115        }
116        public void PlayURLFile(string file)
117        {
118            _mp.Play(new Media(_libVLC, new Uri(file)));
119            isPlaying = true;
120        }
121    }
122}

Form2.Designer.cs

 1namespace SimpleVideoPlayer
 2{
 3    partial class Form2
 4    {
 5        /// <summary>
 6        /// Required designer variable.
 7        /// </summary>
 8        private System.ComponentModel.IContainer components = null;
 9
10        /// <summary>
11        /// Clean up any resources being used.
12        /// </summary>
13        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
14        protected override void Dispose(bool disposing)
15        {
16            if (disposing && (components != null))
17            {
18                components.Dispose();
19            }
20            base.Dispose(disposing);
21        }
22
23        #region Windows Form Designer generated code
24
25        /// <summary>
26        /// Required method for Designer support - do not modify
27        /// the contents of this method with the code editor.
28        /// </summary>
29        private void InitializeComponent()
30        {
31            this.label1 = new System.Windows.Forms.Label();
32            this.textBox1 = new System.Windows.Forms.TextBox();
33            this.button1 = new System.Windows.Forms.Button();
34            this.SuspendLayout();
35            // 
36            // label1
37            // 
38            this.label1.AutoSize = true;
39            this.label1.Location = new System.Drawing.Point(12, 9);
40            this.label1.Name = "label1";
41            this.label1.Size = new System.Drawing.Size(104, 13);
42            this.label1.TabIndex = 0;
43            this.label1.Text = "Paste the URL here:";
44            // 
45            // textBox1
46            // 
47            this.textBox1.Location = new System.Drawing.Point(15, 25);
48            this.textBox1.Name = "textBox1";
49            this.textBox1.Size = new System.Drawing.Size(258, 20);
50            this.textBox1.TabIndex = 1;
51            // 
52            // button1
53            // 
54            this.button1.Location = new System.Drawing.Point(15, 51);
55            this.button1.Name = "button1";
56            this.button1.Size = new System.Drawing.Size(75, 23);
57            this.button1.TabIndex = 2;
58            this.button1.Text = "Open";
59            this.button1.UseVisualStyleBackColor = true;
60            this.button1.Click += new System.EventHandler(this.button1_Click);
61            // 
62            // Form2
63            // 
64            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
65            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
66            this.ClientSize = new System.Drawing.Size(285, 79);
67            this.Controls.Add(this.button1);
68            this.Controls.Add(this.textBox1);
69            this.Controls.Add(this.label1);
70            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
71            this.Name = "Form2";
72            this.Text = "Form2";
73            this.TopMost = true;
74            this.ResumeLayout(false);
75            this.PerformLayout();
76
77        }
78
79        #endregion
80
81        private System.Windows.Forms.Label label1;
82        private System.Windows.Forms.TextBox textBox1;
83        private System.Windows.Forms.Button button1;
84    }
85}

Form2.cs

 1using System;
 2using System.Collections.Generic;
 3using System.ComponentModel;
 4using System.Data;
 5using System.Drawing;
 6using System.Linq;
 7using System.Text;
 8using System.Threading.Tasks;
 9using System.Windows.Forms;
10
11namespace SimpleVideoPlayer
12{
13    public partial class Form2 : Form
14    {
15        public Form2()
16        {
17            InitializeComponent();
18        }
19
20        private void button1_Click(object sender, EventArgs e)
21        {
22            (System.Windows.Forms.Application.OpenForms["Form1"] as Form1).PlayURLFile(textBox1.Text);
23        }
24    }
25}

Conclusion

This is a simple video player, which can be used as a base to create a more complex one. I choose not to make any UI for the player because I want to keep it simple and I want to show you how to make and use shortcuts.

Did you like this tutorial?
Did you find it useful?
Share your thoughts with me on Twitter @CodeSailer