Playback a Video File - React JS

This tutorial shows how to use VideoViewer to perform simplified playback of a video file in a React JS application using the LEADTOOLS Multimedia SDK.

Overview  
Summary This tutorial covers how to use the VideoViewer object in a React JS application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (514 KB)
Platform React JS Web Application
IDE Visual Studio : Service \ Visual Studio Code : Client
Development License Download LEADTOOLS
Try it in another language

Required Knowledge

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 - React JS tutorial.

Make sure that Yarn is installed so that creating a React application can be done quickly via the command line. If yarn is not installed, it can be found on:

https://classic.yarnpkg.com/en/docs/install/#windows-stable

Create the Project and Add LEADTOOLS References

Start with a copy of the project created in the Add References and Set a License tutorial. If you do not have a copy of that tutorial project, follow the steps inside that tutorial to create it.

The references needed depend upon the purpose of the project. For this project, the following JS files are needed:

Make sure to copy these files to the public\common folder and import them in the public\index.html file.

For more information on which files to include for your JavaScript application, see Files to be Included with your Application.

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:

Note

Adding LEADTOOLS local references and setting a license are covered in more detail in the Add References and Set a License tutorial.

Import LEADTOOLS Dependencies

Open the index.html file in the public folder and add the below necessary script tags inside the head to import LEADTOOLS dependencies.

<head> 
    <meta charset="utf-8" /> 
    <link rel="icon" href="%PUBLIC_URL%/favicon.ico" /> 
    <meta name="viewport" content="width=device-width, initial-scale=1" /> 
    <meta name="theme-color" content="#000000" /> 
    <meta 
      name="description" 
      content="Web site created using create-react-app" 
    /> 
    <link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" /> 
     
    <link rel="manifest" href="%PUBLIC_URL%/manifest.json" /> 
     
    <script src="https://code.jquery.com/jquery-3.4.1.min.js"  
    integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script> 
 
    <script src="/common/Leadtools.js"></script> 
    <script src="/common/Leadtools.Annotations.Engine.js"></script>    
    <script src="/common/Leadtools.Document.js"></script>    
    <script src="/common/Leadtools.Document.Viewer.js"></script>  
    <script src="/common/Leadtools.Multimedia.js"></script> 
    <script src="/common/app.js"></script> 
 
    <title>React App</title> 
</head> 

Add the VideoViewer Code

With the project created, the references added, and the license set coding can begin.

Open the App.js file in the src folder and replace the HTML with the following code:

import logo from './logo.svg';  
import './App.css';  
  
function App() {  
  return (  
    <div className="App">  
      <h1 id="serviceStatus">HEADER</h1>  
      <input type="file" id="fileSelect" accept="video/*"/> 
      <button id="addToPlayback">ADD TO PLAYBACK</button>  
      <div id="playbackDiv"> 
        </div> 
    </div>  
  );  
}  
  
export default App;  

Open the app.js file in a text editor. This file should be situated in the /public/common folder within the project structure. Creation of app.js is covered in the Add References and Set a License tutorial.

At the top of the app.js file, before the onload function, add a new function named showServiceError. Add the code below to the new function:

function showServiceError(jqXHR, statusText, errorThrown) { 
    alert("Error returned from service. See the console for details."); 
    let serviceError = lt.Document.ServiceError.parseError(jqXHR, statusText, errorThrown); 
    console.error(serviceError); 
} 

Add the following code below the set license code, inside the onload function, to setup the video viewer and play back selected videos. Note, the onload function must use the async keyword in order to utilize the await operator.

    // Connect to the document service   
    let serviceStatus = document.getElementById('serviceStatus'); 
 
    lt.Document.DocumentFactory.serviceHost = "http://localhost:40000"; 
    lt.Document.DocumentFactory.servicePath = ""; 
    lt.Document.DocumentFactory.serviceApiPath = "api"; 
 
    serviceStatus.innerHTML = "Connecting to " + lt.Document.DocumentFactory.serviceUri; 
 
    await lt.Document.DocumentFactory.verifyService() 
    try { 
        serviceStatus.innerHTML = "Successfully connected to " + lt.Document.DocumentFactory.serviceUri; 
    } catch (error) { 
        console.error(error); 
        serviceStatus.innerHTML = "Failed to connect to " + lt.Document.DocumentFactory.serviceUri; 
    } 
    // Set up video viewer  
    this.videoViewer = new lt.Multimedia.VideoViewer({ root: document.getElementById('playbackDiv') }); 
 
    // Playback selected videos   
    var fileSelect = document.getElementById('fileSelect'); 
    var btn = document.getElementById('addToPlayback'); 
 
    btn.onclick = async () => { 
        if (!fileSelect.files) { 
            return; 
        } 
 
        let videoFile = fileSelect.files[0]; 
        const options = new lt.Multimedia.ConvertVideoOptions(); 
        const safeUrl = await lt.Multimedia.MultimediaFactory.convertVideo(videoFile, options); 
 
        videoViewer.setVideo(safeUrl.toString()); 
    } 

Improve the Visuals of the Project

Navigate to App.css, in the src folder which creates our HTML elements. Add the following code to improve the visuals of the application.

.App {  
  text-align: center;  
  background-color: #61dafb; 
}  
  
@media (prefers-reduced-motion: no-preference) {  
  .App-logo {  
    animation: App-logo-spin infinite 20s linear;  
  }  
}  
  
.App-header {  
  background-color: #282c34;  
  min-height: 100vh;  
  display: flex;  
  flex-direction: column;  
  align-items: center;  
  justify-content: center;  
  font-size: calc(10px + 2vmin);  
  color: white;  
}  
  
.App-link {  
  color: #61dafb;  
}  
  
@keyframes App-logo-spin {  
  from {  
    transform: rotate(0deg);  
  }  
  to {  
    transform: rotate(360deg);  
  }  
}  
  
#addToPlayback{  
 background-color: rgb(195, 250, 241);  
 text-align: center; 
 font-family: Arial, Helvetica, sans-serif; 
 font-weight: 100; 
 width: 350px;  
 padding: 10px;  
 margin: auto; 
 }  
 
 #serviceStatus{  
  background-color: #888888;  
   padding-left: 15px;  
   margin: auto; 
   padding-right: 15px;  
   text-align: center; 
 }  
  
#playbackDiv{  
  background-color: rgba(170, 170, 170, 1);  
  border-radius: 1%;  
  margin: auto; 
  height: 400px;  
  width: 400px;  
} 
 
#fileSelect{ 
  content: 'Select some files'; 
  display: inline-block; 
  background: linear-gradient(top, #f9f9f9, #e3e3e3); 
  border: 1px solid #999; 
  border-radius: 3px; 
  padding: 5px 8px; 
  outline: none; 
  white-space: nowrap; 
  cursor: pointer; 
  text-shadow: 1px 1px #fff; 
  font-weight: 700; 
  font-size: 10pt; 
} 

Run the Document Service

In order to run this application successfully, the LEADTOOLS .NET Framework Document Service is required. The LEADTOOLS .NET Framework Document Service project is located at <INSTALL_DIR>\LEADTOOLS22\Examples\Document\JS\DocumentServiceDotNet\fx.

Note

.NET FrameWork Document Service supports uploadDocumentBlob but .NET Core Document Service does not.

Open the DocumentService.csproj and run the project using IIS Express. After running the csproj Document Service project in Visual Studio, the webpage will show that the service is listening. The Client Side will be able to communicate with the Document Service, allowing the Image Data processing, and returning the OCR's text from the image.

Document service

Run the Project

To run the video playback React application open a new terminal and cd into the root of the project. From there, run yarn start. If you do not have the node modules included with the project, be sure to also run the command, npm install before running the project.

To test, follow the steps below:

Note

AVI file types are no longer supported in HTML.

Wrap-Up

This tutorial showed how to load a video source file and playback the 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.