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 yourvideoId
isGYAB4Td62zI
.[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 theiframe
will attempt to load regardless of the status of the API on the page. Set this to true if you don’t want theonYouTubeIframeAPIReady
field to be set on the globalwindow
Step 4: Import Youtube API script in index.html
<script src="https://www.youtube.com/iframe_api"></script>
No comments:
Post a Comment