This component provides a simple Angular wrapper around the YouTube player API. File any bugs against the angular/components repo.
To install, run ng add @angular/youtube-player
.
Import the component either by adding the YouTubePlayerModule
to your app or by importing YouTubePlayer
into a standalone component. Then add the <youtube-player videoId="<your ID>"
to your template.
If your video is found at https://www.youtube.com/watch?v=mVjYG9TSN88, then your video id is mVjYG9TSN88
.
import{Component}from'@angular/core';import{YouTubePlayer}from'@angular/youtube-player'; @Component({imports: [YouTubePlayer],template: '<youtube-player videoId="mVjYG9TSN88"/>',selector: 'youtube-player-example',})exportclassYoutubePlayerExample{}
Check out the source to read the API.
The <youtube-player/>
component requires the YouTube iframe
to work. If the API isn't loaded by the time the player is initialized, it'll load the API automatically from https://www.youtube.com/iframe_api
. If you don't want it to be loaded, you can either control it on a per-component level using the loadApi
input:
<youtube-playervideoId="mVjYG9TSN88" loadApi="false"/>
Or at a global level using the YOUTUBE_PLAYER_CONFIG
injection token:
import{NgModule}from'@angular/core';import{YouTubePlayer,YOUTUBE_PLAYER_CONFIG}from'@angular/youtube-player'; @NgModule({imports: [YouTubePlayer],providers: [{provide: YOUTUBE_PLAYER_CONFIG,useValue: {loadApi: false}}]})exportclassYourApp{}
By default, the <youtube-player/>
will show a placeholder element instead of loading the API up-front until the user interacts with it. This speeds up the initial render of the page by not loading unnecessary JavaScript for a video that might not be played. Once the user clicks on the video, the API will be loaded and the placeholder will be swapped out with the actual video.
Note that the placeholder won't be shown in the following scenarios:
- Video that plays automatically (e.g.
playerVars
containsautoplay: 1
). - The player is showing a playlist (e.g.
playerVars
contains alist
property).
If you want to disable the placeholder and have the <youtube-player/>
load the API on initialization, you can either pass in the disablePlaceholder
input:
<youtube-playervideoId="mVjYG9TSN88" disablePlaceholder/>
Or set it at a global level using the YOUTUBE_PLAYER_CONFIG
injection token:
import{NgModule}from'@angular/core';import{YouTubePlayer,YOUTUBE_PLAYER_CONFIG}from'@angular/youtube-player'; @NgModule({imports: [YouTubePlayer],providers: [{provide: YOUTUBE_PLAYER_CONFIG,useValue: {disablePlaceholder: true}}]})exportclassYourApp{}
YouTube provides different sizes of placeholder images depending on when the video was uploaded and the thumbnail that was provided by the uploader. The <youtube-player/>
defaults to a quality that should be available for the majority of videos, but if you're seeing a grey placeholder, consider switching to the low
quality using the placeholderImageQuality
input or through the YOUTUBE_PLAYER_CONFIG
.
<!-- Default value, should exist for most videos. --><youtube-playervideoId="mVjYG9TSN88" placeholderImageQuality="standard"/><!-- High quality image that should be present for most videos from the past few years. --><youtube-playervideoId="mVjYG9TSN88" placeholderImageQuality="high"/><!-- Very low quality image, but should exist for all videos. --><youtube-playervideoId="mVjYG9TSN88" placeholderImageQuality="low"/>
Since the placeholder has an interactive button
element, it needs an aria-label
for proper accessibility. The default label is "Play video", but you can customize it based on your app through the placeholderButtonLabel
input or the YOUTUBE_PLAYER_CONFIG
injection token:
<youtube-playervideoId="mVjYG9TSN88" placeholderButtonLabel="Afspil video"/>
There are a couple of considerations when using placeholders:
- Different videos support different sizes of placeholder images and there's no way to know ahead of time which one is supported. The
<youtube-player/>
defaults to a value that should work for most videos, but if you want something higher or lower, you can refer to the "Placeholder image quality" section. - Unlike the native YouTube placeholder, the Angular component doesn't show the video's title, because it isn't known ahead of time.