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
Visual Studio 2017(or later)
LibVLCSharp
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:
Open any video files from the computer.
Open any video files from URL.
Windowed and fullscreen mode options.
Play pause and resume.
Skip forward and backwords.
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:
LibVLCSharp
LibVLCSharp.WinForms
VideoLAN.LibVLC.Windows
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:
Open file…
Open URl…
Exit
Then add a new menu item, call it “Tools” and inside it add:
go Fullscreen
Now you must have something like this:
Adding Code to form1
Now right click on the Form1 and choose “view code”.
At the top add:
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:
publicvoidShortcutEvent(objectsender,KeyEventArgse){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.
Menu strip functions
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=newPoint(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
Form2url_ofd=newForm2();url_ofd.Show();
Open File
This must show a dialog that let us choose a file from our pc.
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:
Now double click on the button, and inside its function paste:
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😄
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;usingLibVLCSharp.Shared;namespaceSimpleVideoPlayer{publicpartialclassForm1:Form{//VLC stuff
publicLibVLC_libVLC;publicMediaPlayer_mp;publicMediamedia;publicboolisFullscreen=false;publicboolisPlaying=false;publicSizeoldVideoSize;publicSizeoldFormSize;publicPointoldVideoLocation;publicForm1(){InitializeComponent();Core.Initialize();this.KeyPreview=true;this.KeyDown+=newKeyEventHandler(ShortcutEvent);oldVideoSize=videoView1.Size;oldFormSize=this.Size;oldVideoLocation=videoView1.Location;//VLC stuff
_libVLC=newLibVLC();_mp=newMediaPlayer(_libVLC);videoView1.MediaPlayer=_mp;}publicvoidShortcutEvent(objectsender,KeyEventArgse){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;}}}privatevoidgoFullscreenToolStripMenuItem_Click(objectsender,EventArgse){menuStrip1.Visible=false;// goodbye menu strip
videoView1.Size=this.Size;// make video the same size as the form
videoView1.Location=newPoint(0,0);// remove the offset
this.FormBorderStyle=FormBorderStyle.None;// cheange form style
this.WindowState=FormWindowState.Maximized;isFullscreen=true;}privatevoidexitToolStripMenuItem_Click(objectsender,EventArgse){Application.Exit();}privatevoidopenURLToolStripMenuItem_Click(objectsender,EventArgse){Form2url_ofd=newForm2();url_ofd.Show();}privatevoidopenToolStripMenuItem_Click(objectsender,EventArgse){OpenFileDialogofd=newOpenFileDialog();if(ofd.ShowDialog()==DialogResult.OK){PlayFile(ofd.FileName);}}publicvoidPlayFile(stringfile){_mp.Play(newMedia(_libVLC,file));isPlaying=true;}publicvoidPlayURLFile(stringfile){_mp.Play(newMedia(_libVLC,newUri(file)));isPlaying=true;}}}
namespaceSimpleVideoPlayer{partialclassForm2{/// <summary>
/// Required designer variable.
/// </summary>
privateSystem.ComponentModel.IContainercomponents=null;/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protectedoverridevoidDispose(booldisposing){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>
privatevoidInitializeComponent(){this.label1=newSystem.Windows.Forms.Label();this.textBox1=newSystem.Windows.Forms.TextBox();this.button1=newSystem.Windows.Forms.Button();this.SuspendLayout();//
// label1
//
this.label1.AutoSize=true;this.label1.Location=newSystem.Drawing.Point(12,9);this.label1.Name="label1";this.label1.Size=newSystem.Drawing.Size(104,13);this.label1.TabIndex=0;this.label1.Text="Paste the URL here:";//
// textBox1
//
this.textBox1.Location=newSystem.Drawing.Point(15,25);this.textBox1.Name="textBox1";this.textBox1.Size=newSystem.Drawing.Size(258,20);this.textBox1.TabIndex=1;//
// button1
//
this.button1.Location=newSystem.Drawing.Point(15,51);this.button1.Name="button1";this.button1.Size=newSystem.Drawing.Size(75,23);this.button1.TabIndex=2;this.button1.Text="Open";this.button1.UseVisualStyleBackColor=true;this.button1.Click+=newSystem.EventHandler(this.button1_Click);//
// Form2
//
this.AutoScaleDimensions=newSystem.Drawing.SizeF(6F,13F);this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;this.ClientSize=newSystem.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
privateSystem.Windows.Forms.Labellabel1;privateSystem.Windows.Forms.TextBoxtextBox1;privateSystem.Windows.Forms.Buttonbutton1;}}
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