Playback a Video File - HTML5 JavaScript

This tutorial shows how to create a JavaScript application that uses the VideoViewer to perform playback of a video file.

Summary This tutorial covers how to play a video file in a HTML5 JavaScript application.
Completion Time 15 minutes
Visual Studio Project Download tutorial project (1 KB)
Platform JS Web Application
IDE Visual Studio Code - Client
Development License Download LEADTOOLS
Try it in another language

Required Knowledge

The JS LEADTOOLS Multimedia Video Viewer Control contains many advanced features that simplify decoding, processing and playing back media from different sources such as files and network streams by passing them as URI to the player.

Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License - HTML5 JavaScript tutorial, before working on the Playback a Video File - HTML5 JavaScript tutorial.

This tutorial makes use of http-server, a console command for running a static file server. To install http-server first install Node.js and then install http-server.

Create the Project and Add LEADTOOLS References

Start with a copy of the project created in the Add References and Set a License - HTML5 JavaScript tutorial. If you do not have that project, follow the steps in that tutorial.

The references needed depend upon the purpose of the project. This tutorial requires the following JS files:

They can be found here: <INSTALL_DIR>\LEADTOOLS22\Bin\JS

Make sure to copy both files to the lib folder and to import them in the index.html file.

Set the License File

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:

Write the HTML File

Open your project's index.html file. Add the below code to import the LEADTOOLS dependencies, LEADTOOLS logic, and set the container for the VideoViewer:

<!DOCTYPE html> 
<html lang="en"> 
    <meta charset="UTF-8" /> 
    <title>Playback a Video File</title> 
    <!--Import LEADTOOLS Dependencies--> 
    <script type="text/javascript" src="./lib/Leadtools.js"></script> 
    <script type="text/javascript" src="./lib/Leadtools.Multimedia.js"></script> 
    <!--Import Project Dependencies--> 
    <script type="text/javascript" src="./app.js"></script> 
   <!--Configure Style for Video Viewer Container-->  
      .container {  
         height: 600px;  
         width: 600px;  
         border: 4px solid black;  
         margin-top: 10px;  
    <!-- DIV with button to load video from URI --> 
       <button id="loadButton">Load Video</button> 
    <!-- DIV to use as the container for the LEADTOOLS video viewer --> 
    <div class="container" id="root">  

Write the JS File

Open your app.js file. Input the code below to create the VideoViewer and playback a sample MP4 file using its URI.

window.onload = function () { 
   // Set License 
   const licenseUrl = "./LEADTOOLS/LEADTOOLS.lic.txt"; 
   const developerKey = "REPLACE_WITH_KEY_CONTENTS"; 
   lt.RasterSupport.setLicenseUri(licenseUrl, developerKey, function ( 
   ) { 
      // Check the status of the license 
      if (setLicenseResult.result) { 
         console.log("LEADTOOLS client license set successfully"); 
      } else { 
         const msg = "LEADTOOLS License is missing, invalid or expired\nError:\n"; 
   // Create a video viewer control in the root Div element 
   this.videoViewer = new lt.Multimedia.VideoViewer({ root: document.getElementById("root") }); 
   // Load a video from URI sample 
   document.getElementById('loadButton').onclick = () => { 

Run the project

Open the command line console, then cd into the root of the project. Use the following command to run a static file server: http-server

The server should start and run on http:localhost:8080. A message should appear in the console indicating all the ports that the server is available on.

Message indicating server is running

Open your browser and navigate to http://localhost:8080/index.html and the video will display and play, as seen below.

Open your browser and navigate to localhost:8080/index.html


This tutorial showed how to playback a video file using the VideoViewer object.

See Also

Help Version 22.0.2024.3.20
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.