Skip to content

WebNinjaDeveloper.com

Programming Tutorials




Menu
  • Home
  • Youtube Channel
  • Official Blog
  • Nearby Places Finder
  • Direction Route Finder
  • Distance & Time Calculator
Menu

Angular 14 Youtube Video Player Example With Controls to Embed Videos Using ngx-youtube-player in TS

Posted on January 17, 2023

 

 

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

 

 

TypeScript
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

 

 

TypeScript
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();
  }
}

 

 

 

 

Recent Posts

  • Android Kotlin Project to Load Image From URL into ImageView Widget
  • Android Java Project to Make HTTP Call to JSONPlaceholder API and Display Data in RecyclerView Using GSON & Volley Library
  • Android Java Project to Download Youtube Video Thumbnail From URL & Save it inside SD Card
  • Android Java Project to Embed Google Maps & Add Markers Using Maps SDK
  • Android Java Project to Download Random Image From Unsplash Using OkHttp & Picasso Library & Display it
  • Angular
  • Bunjs
  • C#
  • Deno
  • django
  • Electronjs
  • java
  • javascript
  • Koajs
  • kotlin
  • Laravel
  • meteorjs
  • Nestjs
  • Nextjs
  • Nodejs
  • PHP
  • Python
  • React
  • ReactNative
  • Svelte
  • Tutorials
  • Vuejs




©2023 WebNinjaDeveloper.com | Design: Newspaperly WordPress Theme