Play Video In Angular Using Ignite UI Video Player

Introduction

The Ignite UI Video Player is an HTML 5 Video Player which renders video on a web page with a robust, cross-browser user interface. It supports H264/AVC video, Ogg/Theora video, and WebM video formats. Ignite UI Video control has many feature like volume control, screenshot, related videos can be added, bookmarks of video can be created, we can set banner ads as well as commercial ads, and more. If you rbrowser does not support HTML 5 then the you can set Microsoft Silverlight media control to enable playback.

Creating Demo

Let's create a demo project by just simply rendering the Ignite UI Video Player control in Angular.

For this Demo project we are using Visual Studio code and Angular. In case you need to set Angular first, you can find help here.

Open Visual Studio code and enter the following command.

ng new (Project Name)

This will create a new blank project for our demo. Now change the directory to the newly-created project and install the angular wrapper by using the following command.

npm install igniteui-angular-wrappers

This will install all the required packages and dependency in the project.

Let's import the required module in src/app/app.module.ts file.

  1. import { IgVideoPlayerComponent } from "igniteui-angular-wrappers";  

And also put “IgVideoPlayerComponent ” in the declaration. Refer to Figure 1.

Angular
Figure 1

 

Now let's replace the html to our video player html tag in src/app/app.component.html 

<ig-video-player widgetId="videoPlayer" [options]="videoPlayer.options"></ig-video-player>

Now open the src/app/app.component.ts file and add the variable which holds the properties of Video Player and initialize that in constructor.

  1. public videoPlayer: any;  
  2.   
  3.  constructor() {  
  4.    this.videoPlayer = {  
  5.      options: {  
  6.        width: '100%',  
  7.        sources: ['https://dl.infragistics.com/pg/2011-1/web/shared/videoplayer/videos/Infragistics_Presentation_lowRes_1.h264.mp4',  
  8.          'https://dl.infragistics.com/pg/2011-1/web/shared/videoplayer/videos/Infragistics_Presentation_lowRes_1.webmvp8.webm',  
  9.          'https://dl.infragistics.com/pg/2011-1/web/shared/videoplayer/videos/Infragistics_Presentation_lowRes_1.theora.ogv']  
  10.      }  
  11.    };  
  12.  }  

The last step is to add the CSS files and Java Scripts file to the project which is provided by Infragistics.

Go to the src/index.html and add the following under the head section.

  1. <!-- Ignite UI Required Combined CSS Files -->  
  2.  <link href="https://cdn-na.infragistics.com/igniteui/latest/css/themes/infragistics/infragistics.theme.css" rel="stylesheet"  
  3.   />  
  4.   <link href="https://cdn-na.infragistics.com/igniteui/latest/css/structure/infragistics.css" rel="stylesheet" />  
  5.   
  6.   <script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>  
  7.   <script src="https://code.jquery.com/ui/1.11.1/jquery-ui.min.js"></script>  
  8.   
  9.   <!-- Ignite UI Required Combined JavaScript Files -->  
  10.   <script src="https://cdn-na.infragistics.com/igniteui/latest/js/infragistics.core.js"></script>  
  11.   <script src="https://cdn-na.infragistics.com/igniteui/latest/js/infragistics.lob.js"></script>  
  12.   <script src="https://cdn-na.infragistics.com/igniteui/latest/js/infragistics.dv.js"></script>  

Save all files and open the terminal. To test the project hit the command

 ng server --open

This will build the project and open the default browser and show the output of the project. Refer to Figure 2.

Angular
Figure 2

 

Ignite UI Video Player has the following features, 

  1. Video Player Controls
  2. Commercial Ads
  3. Bookmarks
  4. Banners
  5. Related Videos
Video Player Controls

To perform Ignite UI Video player actions manually (I e Play, Pause, Add Screen shot etc), go to the app.component.ts file and import “ViewChild” in header from “@angular/core”

Go to the app.componet .Html file and update the video player tag like this:

“<ig-video-player widgetId="videoPlayer" [options]="videoPlayer.options" #vp1></ig-video-player>”

Notice the # tag in the video control which help us to find the control in component.ts file. We set the value to vp1.

Add another button which is used to play/pause the video:

  1. <button (click)="playPause($event.target)" id="btnPlay" class="buttons" type="button">  
  2.     Play  
  3. </button>  

Go back to the app.component.ts file and make the function after constructor as follows:

  1. playPause(e) {  
  2.    if (this.isVpPlaying) {  
  3.      this.vp1.pause();  
  4.      this.isVpPlaying = false;  
  5.      e.textContent = ''Play'';  
  6.    } else {  
  7.      this.vp1.play();  
  8.      this.isVpPlaying = true;  
  9.      e.textContent = Pause;  
  10.    }  
  11.  }  

This is a simple function in which we are passing a button to function and changing the text accordingly. We also declare a simple bool called “isVpPlaying” which controls the play/pause action. Save the changes and go back to the browser. Refer to Figure 3:

Angular
Figure 3

 

To add screenshot functionality in the app go to the app.component.Html file and add another button for screenshot. 

  1. <button (click)="addScreenShot()" id="btnAddScreenShot" class="buttons" type="button">  
  2.    Add screenshot  
  3.  </button>  

Add another div which renders the screen shot at runtime:

  1. <div ng-if="screenShotAvail">  
  2.   <div id="scrrenShotDiv" #screenShotDivEle>  
  3.   </div>  
  4. </div>  

We will set the if condition on div which stops rendering if the bool is false (optional).

Now go to the app.component.ts file and add the ElementRefin the header.

import { Component, ViewChild, ElementRef } from '@angular/core'

Add the variable to refer to the div element.

  1. @ViewChild('screenShotDivEle') div: ElementRef;  

Make a new function as follows:

  1. addScreenShot() {  
  2.    const cn = this.div.nativeElement;  
  3.    const canvas1 = this.vp1.screenshot();  
  4.    this.screenShotAvail = true;  
  5.    cn.appendChild(canvas1);  
  6.  }  

Angular
Figure 4

 

Commercial Ads

To add commercial ads in videos go to app.component.ts. In the constructor function where we define all properties of video add the following code:

  1. commercials: {  
  2.        linkedCommercials: [  
  3.          {  
  4.            sources: [  
  5.              'https://dl.infragistics.com/pg/2011-1/web/shared/videoplayer/videos/QuinceIntro_1.h264.mp4',  
  6.              'https://dl.infragistics.com/pg/2011-1/web/shared/videoplayer/videos/QuinceIntro_1.webmvp8.webm',  
  7.              'https://dl.infragistics.com/pg/2011-1/web/shared/videoplayer/videos/QuinceIntro_1.theora.ogv'],  
  8.            startTime: 20,  
  9.            title: 'Quince<br/>Presentation<br/>Part 1',  
  10.            link: 'http://www.infragistics.com/'  
  11.          },  
  12.          {  
  13.            sources: [  
  14.              'https://dl.infragistics.com/pg/2011-1/web/shared/videoplayer/videos/QuinceIntro_Part3_1.h264.mp4',  
  15.              'https://dl.infragistics.com/pg/2011-1/web/shared/videoplayer/videos/QuinceIntro_Part3_1.webmvp8.webm',  
  16.              'https://dl.infragistics.com/pg/2011-1/web/shared/videoplayer/videos/QuinceIntro_Part3_1.theora.ogv'],  
  17.            startTime: 100,  
  18.            title: 'Quince<br/>Presentation<br/>Part 2',  
  19.            link: 'https://www.infragistics.com/'  
  20.          }],  
  21.        adMessage: {  
  22.          hideDelay: 13000  
  23.        }  
  24.      }  

This Commercial section needs to be added under the “option” section property. For output refer to Figure 5.

Angular
Figure 5

 

Bookmark

To add bookmarks in the video, go to the app.component.ts and add the following code.

  1. bookmarks: [  
  2.          {  
  3.            title: 'River',  
  4.            description: 'River',  
  5.            time: 13  
  6.          },  
  7.          {  
  8.            title: 'Big Buck Bunny Appears',  
  9.            description: 'Big Buck Bunny Appears',  
  10.            time: 33  
  11.          }  
  12.        ]  

This bookmarks section need to be added under the “option” section property. For output refer to Figure 6.

Angular
Figure 6

 

Banner

To add the the banner on the video go to the app.component.ts and add the following code. 

  1. banners: [{  
  2.   imageUrl: 'https://www.igniteui.com/images/samples/video-player/banner.png',  
  3.   link: 'https://www.infragistics.com/',  
  4.   times: [5, 20, 60],  
  5.   visible: true,  
  6.   closeBanner: true,  
  7.   animate: true,  
  8.   autohide: true,  
  9.   hidedelay: 10000,  
  10.   width: 270,  
  11.   height: 67  
  12. }],  

This bookmarks section needs to be added under the “option” section property. For output refer to Figure 7.

Angular
Figure 7

 

Related Videos

To add the the Related video’s url in the end of video, go to the app.component.ts and add the following code.

  1. relatedVideos: [  
  2.          {  
  3.            imageUrl: 'https://vassallohistory.files.wordpress.com/2013/09/adelphi-rabat.jpg?w=100',  
  4.            title: 'Quince Presentation Part 1',  
  5.            css: 'relatedVideosBanner',  
  6.            sources: ['https://dl.infragistics.com/pg/2011-1/web/shared/videoplayer/videos/QuinceIntro_Part3_1.h264.mp4',  
  7.              'https://dl.infragistics.com/pg/2011-1/web/shared/videoplayer/videos/QuinceIntro_Part3_1.webmvp8.webm',  
  8.              'https://dl.infragistics.com/pg/2011-1/web/shared/videoplayer/videos/QuinceIntro_Part3_1.theora.ogv'  
  9.            ]  
  10.          }  
  11.        ]  

This related videos section needs to be added under  the “option” section property. For output refer to Figure 8.

Angular
Figure 8

Summary

In this tutorial, we saw how to implement video player on a website using Ignite UI Video player. To learn more and download, please visit Ignite UI Video Player sample page here. 
 


Similar Articles