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)

🌚 Friends don't let friends browse without dark mode.

Sorry, it's true.