|
| 1 | +# Angular YouTube Player component |
| 2 | + |
| 3 | +This component provides a simple angular wrapper around the embed [YouTube player API](https://developers.google.com/youtube/iframe_api_reference). File any bugs against the [angular/components repo](https://github.com/angular/components/issues). |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +To install, run `npm install @angular/youtube-player`. |
| 8 | + |
| 9 | +## Usage |
| 10 | + |
| 11 | +Follow the following instructions for setting up the YouTube player component: |
| 12 | + |
| 13 | +- First, follow the [instructions for installing the API script](https://developers.google.com/youtube/iframe_api_reference#Getting_Started). |
| 14 | +- Then make sure the API is available before bootstraping the YouTube Player component. |
| 15 | +- Provide find the video id by extracting it from the video URL. |
| 16 | + |
| 17 | +## Example |
| 18 | + |
| 19 | +If your video is found at https://www.youtube.com/watch?v=PRQCAL_RMVo, then your video id is `PRQCAL_RMVo`. |
| 20 | + |
| 21 | +```typescript |
| 22 | +// example-module.ts |
| 23 | +import {CommonModule} from '@angular/common'; |
| 24 | +import {NgModule} from '@angular/core'; |
| 25 | +import {YouTubePlayerModule} from '@angular/youtube-player'; |
| 26 | +import {YoutubePlayerExample} from './example-component'; |
| 27 | + |
| 28 | +@NgModule({ |
| 29 | + imports: [ |
| 30 | + // Needed for ngIf directive. |
| 31 | + CommonModule, |
| 32 | + YouTubePlayerModule, |
| 33 | + ], |
| 34 | + declarations: [YoutubePlayerExample], |
| 35 | +}) |
| 36 | +export class YoutubePlayerExampleModule { |
| 37 | +} |
| 38 | + |
| 39 | +// example-component.ts |
| 40 | +@Component({ |
| 41 | + moduleId: module.id, |
| 42 | + template: '<youtube-player videoId="PRQCAL_RMVo" />', |
| 43 | + selector: 'youtube-player-example', |
| 44 | +}) |
| 45 | +class YoutubePlayerExample implements OnInit { |
| 46 | + onInit() { |
| 47 | + // This code loads the IFrame Player API code asynchronously, according to the instructions at |
| 48 | + // https://developers.google.com/youtube/iframe_api_reference#Getting_Started |
| 49 | + const tag = document.createElement('script'); |
| 50 | + |
| 51 | + tag.src = "https://www.youtube.com/iframe_api"; |
| 52 | + document.body.appendChild(tag); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +``` |
| 57 | + |
| 58 | +## API |
| 59 | + |
| 60 | +Check out the [source](./youtube-player.ts) to read the API. |
0 commit comments