DEV Community

Pandiyan Murugan
Pandiyan Murugan

Posted on • Originally published at efficientuser.com

Angular 9 – Youtuber Player Component

Originally posted in my blog

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 effort to embed and youtube videos.

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 reusable components in angular 9.

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

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

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

How to render a Youtube video in the angular application?

Step 1: Install the youtube package

You install the youtube player npm package with the 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 into 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 a 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 a 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](https://www.youtube.com/watch?v=GYAB4Td62zI).
  • [height]: number — the 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

https://efficientuser.files.wordpress.com/2020/03/image.png?w=527

Step 0: Watch the demo and code

Demo

Top comments (0)