This tutorial shows how to use the PlayCtrl
to perform simplified playback of a video file in a WinForms C# application using the LEADTOOLS Multimedia SDK.
Overview | |
---|---|
Summary | This tutorial covers how to play a video file in a WinForms C# application. |
Completion Time | 30 minutes |
Visual Studio Project | Download tutorial project (4 KB) |
Platform | WinForms C# Application |
IDE | Visual Studio 2022 |
Development License | Download LEADTOOLS |
Try it in another language |
|
The LEADTOOLS Multimedia Play Control contains many advanced features that simplify decoding, processing and playing back media from different sources such as files, memory buffers and network streams. The toolkit is shipped with various demos that make use of these features, such as the main Multimedia Player demo that has different editions in multiple programming languages.
Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial, before working on the Playback a Video File - WinForms C# tutorial.
Start with a copy of the project created in the Add References and Set a License tutorial. If you do not have that project, follow the steps in that tutorial to create it.
If using NuGet packages, the following NuGet packages are required:
Leadtools.Formats.Raster.Common
Leadtools.Multimedia
If not using NuGet packages, this tutorial will require the following local DLLs, which are located at <INSTALL_DIR>\LEADTOOLS23\Bin\net
:
Leadtools.dll
Leadtools.Multimedia.dll
Note
Different SDK features require different references. For a complete list, refer to Multimedia Files You Must Include With Your Application.
The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details, including tutorials for different platforms, refer to Setting a Runtime License.
There are two types of runtime licenses:
Note
Adding LEADTOOLS NuGet and local references and setting a license are covered in more detail in the Add References and Set a License tutorial.
With the project created, the references added, the toolkit unlocked, and the license set, coding can begin.
In Solution Explorer, right-click on Form1.cs
and select View Code to display the code-behind of the form. Add the following statements to the using
block at the top.
// Using block at the top
using System;
using System.IO;
using System.Windows.Forms;
using Leadtools;
using Leadtools.Multimedia;
Add the below code to initialize the Play Control.
// Add this global variable
private PlayCtrl _play;
In Solution Explorer, double-click Form1.cs
to display it in the Designer. Click the Events icon in the Properties Windows. Then, double-click the Load event to create an event handler.
Add the following code inside the Form1_Load
event handler.
private void Form1_Load(object sender, EventArgs e)
{
SetLicense();
_play = new PlayCtrl();
_play.Dock = DockStyle.Fill;
Controls.Add(_play);
_play.BringToFront();
}
Open Form1.cs
in the Designer, then add a File menu with an Open menu item. To do that, open the Toolbox and double-click MenuStrip, which will add a menu to the form.
In the Designer, change the text of the menu to &File, which will underline the F in File. Then add an item to the menu and set its text to &Open. Leave the new item's name as openToolStripMenuItem
.
Open the form's Designer and double-click the Open
menu item to edit its event handler. Add the following code in it:
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.InitialDirectory = @"C:\LEADTOOLS23\Resources\Media";
if (dlg.ShowDialog(this) == DialogResult.OK)
{
string inputFile = dlg.FileName;
_play.SourceFile = inputFile;
_play.Run();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
Run the project by pressing F5, or by selecting Debug -> Start Debugging.
If the steps were followed correctly, the application runs and allows loading and playback of any media file using the Open
dialog box.
This tutorial showed how to playback a video file using the PlayCtrl
class.