Welcome folks today in this blog post we will be showing the youtube video
in angular 14 with controls
using the ngx-youtube-player
library in browser using typescript. All the full source code of the application is shown below.
Get Started
In order to get started you need to create a new angular
project using the below command
ng new sampleproject
And after that you need to install the below libraries using the below command
npm i ngx-youtube-player
And after that you will see the below directory
structure of the angular app
Now we need to go to app.module.ts
file and copy paste the below code
app.module.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { FormsModule } from '@angular/forms'; import { AppComponent } from './app.component'; import { NgxYoutubePlayerModule } from 'ngx-youtube-player'; @NgModule({ imports: [ BrowserModule, FormsModule, NgxYoutubePlayerModule.forRoot() ], declarations: [ AppComponent ], bootstrap: [ AppComponent ] }) export class AppModule { } |
After that we need to go to app.component.html
file and copy paste the following code
app.component.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
<div class="container"> <div class="jumbotron bg-light"> <h1 class="display-6">Angular v{{ version }} Youtube Player Component</h1> <p class="lead">This is a simple usage if ngx-youtube-player component.</p> <hr class="my-4"> <section class="card card-info text-white bg-dark mb-3"> <div class="card-header">Demo</div> <section class="card-img-top"> <youtube-player [videoId]="id" (ready)="savePlayer($event)" (change)="onStateChange($event)" [playerVars]="playerVars"> </youtube-player> </section> <div class="card-body"></div> </section> <button *ngIf="ytEvent !== 1" type="button" class="btn btn-primary" (click)="playVideo()"> Play </button> <button *ngIf="ytEvent === 1" type="button" class="btn btn-danger" (click)="pauseVideo()"> Pause </button> </div> </div> <div class="col-12"> <section class="card card-success"> <div class="card-body"> <div class="card-title">Player State</div> <pre>{{ ytEvent }}</pre> </div> </section> </div> |
As you can see we are embeding
or displaying the youtube video inside the bootstrap
player and also using the ngx-youtube-video
player and for this inside the index.html
file you need to copy paste the cdn of bootstrap
as shown below
index.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<!DOCTYPE html> <html> <head> <title>Angular Playground</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/latest/css/bootstrap.min.css" type="text/css" /> </head> <body> <my-app> loading... </my-app> </body> </html> |
And now you need to go to app.component.ts
file and copy paste the below typescript
code to add the functionality
to the youtube video player to play
and pause
the video
app.component.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
import { Component } from '@angular/core'; import * as NGYTPackage from '../../package.json'; @Component({ selector: 'my-app', templateUrl: './app.component.html', styleUrls: ['./app.component.css'], }) export class AppComponent { id = 'V462IsOV3js'; playerVars = { cc_lang_pref: 'en', }; version = '...'; private player; public ytEvent; constructor() { this.version = NGYTPackage['dependencies']['ngx-youtube-player'].replace( '^', '' ); } onStateChange(event) { this.ytEvent = event.data; } savePlayer(player) { this.player = player; } playVideo() { this.player.playVideo(); } pauseVideo() { this.player.pauseVideo(); } } |