Thursday, May 20, 2021

Angular 9 – YouTube Player Component

 Embedding a YouTube video into your angular application isn’t a straight forward work before angular 9.

Before angular 8.2, it needs lots of efforts to embed and YouTube video.

You need an npm plugin to do the operation. Or you might need to iframe and a URL which is sanitized.

What new in Angular 9?

Angular Team tried to reduce the complexity of building a reusable components in angular 9.

They have released lots of interesting components/Module like ClipboardModule, Google maps, YouTube player component in the angular 9.

Earlier, we have discussed about copy to clipboard CDK in our website.

In this post, we are going to discuss on how to embed a YouTube video in angular application in quick steps.

How to render a YouTube video in angular application?

Step 1: Install the YouTube package

You install the YouTube player npm package with following command at the home directory of your application.

npm install @angular/youtube-player

Step 2: Import YouTube Player modules

Import the YouTube Player modules in to your app .module.ts (or you can import in your target module)

import { YouTubePlayerModule } from "@angular/youtube-player";

...

 imports: [
    ....,
    YouTubePlayerModule,
    ...
  ],

Step 3: Add YouTube Player component in HTML

We can add YouTube player component in our target component as below with the YouTube video ID.

We can easily get any id of and YouTube video. The ID of YouTube video will present in the URL itself.

Example:

https://www.youtube.com/watch?v=GYAB4Td62zI

In this URL, GYAB4Td62zI is the id of the video.

<youtube-player 
  videoId="GYAB4Td62zI" 
  suggestedQuality="highres" 
  [height]="250" 
  [width]="500" 
  [startSeconds]="4"
  [endSeconds]="8">
</youtube-player>

Parameters details

  • [videoId]: string — YouTube Video ID to render. It’s the little hash at the end of the YouTube URL. For example, if your video is found at https://www.youtube.com/watch?v=GYAB4Td62zI, then your videoId is GYAB4Td62zI.
  • [height]: number — height of video player
  • [width]: number — width of video player
  • [startSeconds]: number — the moment when the player is supposed to start playing
  • [endSeconds]: number— the moment when the player is supposed to stop playing
  • [suggestedQuality]:— the suggested quality of the player. This can take the values 'default' , 'small''medium''large''hd720''hd1080', and'highres'
  • [showBeforeIframeApiLoads]: boolean— whether the iframe will attempt to load regardless of the status of the API on the page. Set this to true if you don’t want the onYouTubeIframeAPIReady field to be set on the global window

Step 4: Import Youtube API script in index.html

<script src="https://www.youtube.com/iframe_api"></script>

Step 5: Run the code and see the magic

Step 0: Watch the demo and code

Demo

No comments:

Post a Comment

Technology’s generational moment with generative AI: A CIO and CTO guide

 Ref:  A CIO and CTO technology guide to generative AI | McKinsey 1. Determine the company’s posture for the adoption of generative AI As us...