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:

1
using LibVLCSharp.Shared;

Inside the form1 class add these variables:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
//VLC stuff
public LibVLC _libVLC;
public MediaPlayer _mp;
public Media media;

public bool isFullscreen = false;
public bool isPlaying = false;
public Size oldVideoSize;
public Size oldFormSize;
public Point oldVideoLocation;

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

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
public void ShortcutEvent(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Escape && isFullscreen) // from fullscreen to window
    {
        this.FormBorderStyle = FormBorderStyle.Sizable; // change form style
        this.WindowState = FormWindowState.Normal; // back to normal size
        this.Size = oldFormSize;
        menuStrip1.Visible = true; // the return of the menu strip 
        videoView1.Size = oldVideoSize; // make video the same size as the form
        videoView1.Location = oldVideoLocation; // remove the offset
        isFullscreen = false;
    }

    if (isPlaying) // while the video is playing
    {
        if (e.KeyCode == Keys.Space) // Pause and Play
        {
            if (_mp.State == VLCState.Playing) // if is playing
            {
                _mp.Pause(); // pause
            }
            else // it's not playing?
            {
                _mp.Play(); // play
            }
        }

        if (e.KeyCode == Keys.J) // skip 1% backwards
        {
            _mp.Position -= 0.01f;
        }
        if (e.KeyCode == Keys.L) // skip 1% forwards
        {
            _mp.Position += 0.01f;
        }
    }
}

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:

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

1
Application.Exit();

Open URL

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

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

Open File

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

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

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:

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

PlayURLFile Function

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

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

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

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
namespace SimpleVideoPlayer
{
    partial class Form1
    {

        private System.ComponentModel.IContainer components = null;

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.videoView1 = new LibVLCSharp.WinForms.VideoView();
            this.menuStrip1 = new System.Windows.Forms.MenuStrip();
            this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.openToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.openURLToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.toolsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.goFullscreenToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            ((System.ComponentModel.ISupportInitialize)(this.videoView1)).BeginInit();
            this.menuStrip1.SuspendLayout();
            this.SuspendLayout();
            // 
            // videoView1
            // 
            this.videoView1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.videoView1.BackColor = System.Drawing.Color.Black;
            this.videoView1.Location = new System.Drawing.Point(0, 27);
            this.videoView1.MediaPlayer = null;
            this.videoView1.Name = "videoView1";
            this.videoView1.Size = new System.Drawing.Size(800, 444);
            this.videoView1.TabIndex = 0;
            this.videoView1.Text = "videoView1";
            // 
            // menuStrip1
            // 
            this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.fileToolStripMenuItem,
            this.toolsToolStripMenuItem});
            this.menuStrip1.Location = new System.Drawing.Point(0, 0);
            this.menuStrip1.Name = "menuStrip1";
            this.menuStrip1.Size = new System.Drawing.Size(800, 24);
            this.menuStrip1.TabIndex = 1;
            this.menuStrip1.Text = "menuStrip1";
            // 
            // fileToolStripMenuItem
            // 
            this.fileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.openToolStripMenuItem,
            this.openURLToolStripMenuItem,
            this.exitToolStripMenuItem});
            this.fileToolStripMenuItem.Name = "fileToolStripMenuItem";
            this.fileToolStripMenuItem.Size = new System.Drawing.Size(37, 20);
            this.fileToolStripMenuItem.Text = "File";
            // 
            // openToolStripMenuItem
            // 
            this.openToolStripMenuItem.Name = "openToolStripMenuItem";
            this.openToolStripMenuItem.Size = new System.Drawing.Size(136, 22);
            this.openToolStripMenuItem.Text = "Open File...";
            this.openToolStripMenuItem.Click += new System.EventHandler(this.openToolStripMenuItem_Click);
            // 
            // openURLToolStripMenuItem
            // 
            this.openURLToolStripMenuItem.Name = "openURLToolStripMenuItem";
            this.openURLToolStripMenuItem.Size = new System.Drawing.Size(136, 22);
            this.openURLToolStripMenuItem.Text = "Open URL...";
            this.openURLToolStripMenuItem.Click += new System.EventHandler(this.openURLToolStripMenuItem_Click);
            // 
            // exitToolStripMenuItem
            // 
            this.exitToolStripMenuItem.Name = "exitToolStripMenuItem";
            this.exitToolStripMenuItem.Size = new System.Drawing.Size(136, 22);
            this.exitToolStripMenuItem.Text = "Exit";
            this.exitToolStripMenuItem.Click += new System.EventHandler(this.exitToolStripMenuItem_Click);
            // 
            // toolsToolStripMenuItem
            // 
            this.toolsToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.goFullscreenToolStripMenuItem});
            this.toolsToolStripMenuItem.Name = "toolsToolStripMenuItem";
            this.toolsToolStripMenuItem.Size = new System.Drawing.Size(46, 20);
            this.toolsToolStripMenuItem.Text = "Tools";
            // 
            // goFullscreenToolStripMenuItem
            // 
            this.goFullscreenToolStripMenuItem.Name = "goFullscreenToolStripMenuItem";
            this.goFullscreenToolStripMenuItem.Size = new System.Drawing.Size(143, 22);
            this.goFullscreenToolStripMenuItem.Text = "Go fullscreen";
            this.goFullscreenToolStripMenuItem.Click += new System.EventHandler(this.goFullscreenToolStripMenuItem_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(800, 464);
            this.Controls.Add(this.videoView1);
            this.Controls.Add(this.menuStrip1);
            this.MainMenuStrip = this.menuStrip1;
            this.Name = "Form1";
            this.Text = "Form1";
            ((System.ComponentModel.ISupportInitialize)(this.videoView1)).EndInit();
            this.menuStrip1.ResumeLayout(false);
            this.menuStrip1.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private LibVLCSharp.WinForms.VideoView videoView1;
        private System.Windows.Forms.MenuStrip menuStrip1;
        private System.Windows.Forms.ToolStripMenuItem fileToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem openToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem openURLToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem exitToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem toolsToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem goFullscreenToolStripMenuItem;
    }
}

Form1.cs

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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 LibVLCSharp.Shared;

namespace SimpleVideoPlayer
{
    public partial class Form1 : Form
    {
        //VLC stuff
        public LibVLC _libVLC;
        public MediaPlayer _mp;
        public Media media;

        public bool isFullscreen = false;
        public bool isPlaying = false;
        public Size oldVideoSize;
        public Size oldFormSize;
        public Point oldVideoLocation;

        public Form1()
        {
            InitializeComponent();
            Core.Initialize();
            this.KeyPreview = true;
            this.KeyDown += new KeyEventHandler(ShortcutEvent);
            oldVideoSize = videoView1.Size;
            oldFormSize = this.Size;
            oldVideoLocation = videoView1.Location;
            //VLC stuff
            _libVLC = new LibVLC();
            _mp = new MediaPlayer(_libVLC);
            videoView1.MediaPlayer = _mp;
        }

        public void ShortcutEvent(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape && isFullscreen) // from fullscreen to window
            {
                this.FormBorderStyle = FormBorderStyle.Sizable; // change form style
                this.WindowState = FormWindowState.Normal; // back to normal size
                this.Size = oldFormSize;
                menuStrip1.Visible = true; // the return of the menu strip 
                videoView1.Size = oldVideoSize; // make video the same size as the form
                videoView1.Location = oldVideoLocation; // remove the offset
                isFullscreen = false;
            }

            if (isPlaying) // while the video is playing
            {
                if (e.KeyCode == Keys.Space) // Pause and Play
                {
                    if (_mp.State == VLCState.Playing)
                    {
                        _mp.Pause();
                    }
                    else
                    {
                        _mp.Play();
                    }
                }

                if (e.KeyCode == Keys.J) // skip 1% backwards
                {
                    _mp.Position -= 0.01f;
                }
                if (e.KeyCode == Keys.L) // skip 1% forwards
                {
                    _mp.Position += 0.01f;
                }
            }
        }

        private void goFullscreenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            menuStrip1.Visible = false; // goodbye menu strip
            videoView1.Size = this.Size; // make video the same size as the form
            videoView1.Location = new Point(0, 0); // remove the offset
            this.FormBorderStyle = FormBorderStyle.None; // cheange form style
            this.WindowState = FormWindowState.Maximized;
            isFullscreen = true;

        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void openURLToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form2 url_ofd = new Form2();
            url_ofd.Show();
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if(ofd.ShowDialog() == DialogResult.OK)
            {
                PlayFile(ofd.FileName);
            }
        }

        public void PlayFile(string file)
        {
            _mp.Play(new Media(_libVLC, file));
            isPlaying = true;
        }
        public void PlayURLFile(string file)
        {
            _mp.Play(new Media(_libVLC, new Uri(file)));
            isPlaying = true;
        }
    }
}

Form2.Designer.cs

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
namespace SimpleVideoPlayer
{
    partial class Form2
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(12, 9);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(104, 13);
            this.label1.TabIndex = 0;
            this.label1.Text = "Paste the URL here:";
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(15, 25);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(258, 20);
            this.textBox1.TabIndex = 1;
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(15, 51);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 2;
            this.button1.Text = "Open";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // Form2
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(285, 79);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.label1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.Name = "Form2";
            this.Text = "Form2";
            this.TopMost = true;
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Button button1;
    }
}

Form2.cs

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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;

namespace SimpleVideoPlayer
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            (System.Windows.Forms.Application.OpenForms["Form1"] as Form1).PlayURLFile(textBox1.Text);
        }
    }
}

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