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

To get latest updates you can follow or subscribe! #peace

Advertisement

3 thoughts on “Angular 9 – YouTube Player Component

  1. Jelle January 10, 2021 / 6:06 pm

    Thx for the post. I followed your instructions, made sure that I have the right versions of core and common but still get a error NG8001: ‘youtube-player’ is not a known element:

    Like

  2. nirmal February 13, 2021 / 7:11 pm

    how to make it responsive

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s