You do not need any wrapper, including itself, it is not.
1.) include the youtube iFrame API through the script tag.
ngAfterViewInit() { const doc = (<any>window).document; let playerApiScript = doc.createElement('script'); playerApiScript.type = 'text/javascript'; playerApiScript.src = 'https://www.youtube.com/iframe_api'; doc.body.appendChild(playerApiScript); }
2.) register your callback inside onYouTubeIframeAPIReady () , which the Youtube API will call when the page finishes loading the JavaScript for the playerβs API.
ngOnInit() { (<any>window).onYouTubeIframeAPIReady = () => { this.player = new (<any>window).YT.Player('ytplayer', { height: '100%', width: '100%', videoId: 'YourVideoId', playerVars: {'autoplay': 1, 'rel': 0, 'controls': 2}, events: { 'onReady': () => { }, 'onStateChange': () => { } } }); }; }
You can also set autoplay to 0 so that it does not play at boot.
Now we have this.player at the component level, and you can do other manipulations, for example:
- pause:
this.player.pauseVideo(); , - upload another video
this.player.loadVideoById('someOtherVideoId') etc.
source share