Skip to content

Commit 6aec6a7

Browse files
committed
Youtube loading iframe issue
1 parent f2dcc84 commit 6aec6a7

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

src/app/radio/components/player-page/player-page.component.ts

+17-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { AfterViewInit, Component, EventEmitter, Input, OnChanges, OnInit, Output, SimpleChanges } from '@angular/core';
1+
import { AfterViewInit, Component, EventEmitter, Input, OnChanges, OnDestroy, OnInit, Output, SimpleChanges } from '@angular/core';
22
import { Observable, distinctUntilChanged, take } from 'rxjs';
33
import { PlayerMainService } from 'src/app/services/player-main.service';
44
declare var YT: any; // Add this line to declare the type of YT as any
@@ -8,23 +8,33 @@ declare var YT: any; // Add this line to declare the type of YT as any
88
templateUrl: './player-page.component.html',
99
styleUrls: ['./player-page.component.scss']
1010
})
11-
export class PlayerPageComponent implements OnInit, OnChanges {
11+
export class PlayerPageComponent implements OnInit, OnChanges, OnDestroy {
1212
@Input() youtubeId!: string;
1313
playPause$!: Observable<boolean>;
1414
apiLoaded = false;
1515
player: any;
1616

1717
constructor(private _playerMainService: PlayerMainService) {
1818
}
19+
ngOnDestroy(): void {
20+
this.player.destroy;
21+
}
1922

2023
ngOnInit() {
2124
this.playPause$ = this._playerMainService.playPause$;
22-
(window as any).onYouTubeIframeAPIReady = () => {
25+
if ((window as any)['YT'] && YT.Player) {
2326
if (this.youtubeId) {
2427
this.loadVideo(this.youtubeId);
2528
}
26-
};
27-
this.loadYouTubeAPI();
29+
} else {
30+
this.loadYouTubeAPI();
31+
(window as any).onYouTubeIframeAPIReady = () => {
32+
if (this.youtubeId) {
33+
this.loadVideo(this.youtubeId);
34+
}
35+
};
36+
}
37+
2838
}
2939

3040
ngOnChanges(changes: SimpleChanges): void {
@@ -38,8 +48,10 @@ ngOnChanges(changes: SimpleChanges): void {
3848

3949
loadYouTubeAPI() {
4050
const tag = document.createElement('script');
51+
tag.id = "youtubeApiScript"
4152
tag.src = 'https://www.youtube.com/iframe_api';
4253
document.body.appendChild(tag);
54+
4355
}
4456

4557
loadVideo(vidID: string) {

0 commit comments

Comments
 (0)