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' ;
2
2
import { Observable , distinctUntilChanged , take } from 'rxjs' ;
3
3
import { PlayerMainService } from 'src/app/services/player-main.service' ;
4
4
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
8
8
templateUrl : './player-page.component.html' ,
9
9
styleUrls : [ './player-page.component.scss' ]
10
10
} )
11
- export class PlayerPageComponent implements OnInit , OnChanges {
11
+ export class PlayerPageComponent implements OnInit , OnChanges , OnDestroy {
12
12
@Input ( ) youtubeId ! : string ;
13
13
playPause$ ! : Observable < boolean > ;
14
14
apiLoaded = false ;
15
15
player : any ;
16
16
17
17
constructor ( private _playerMainService : PlayerMainService ) {
18
18
}
19
+ ngOnDestroy ( ) : void {
20
+ this . player . destroy ;
21
+ }
19
22
20
23
ngOnInit ( ) {
21
24
this . playPause$ = this . _playerMainService . playPause$ ;
22
- ( window as any ) . onYouTubeIframeAPIReady = ( ) => {
25
+ if ( ( window as any ) [ 'YT' ] && YT . Player ) {
23
26
if ( this . youtubeId ) {
24
27
this . loadVideo ( this . youtubeId ) ;
25
28
}
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
+
28
38
}
29
39
30
40
ngOnChanges ( changes : SimpleChanges ) : void {
@@ -38,8 +48,10 @@ ngOnChanges(changes: SimpleChanges): void {
38
48
39
49
loadYouTubeAPI ( ) {
40
50
const tag = document . createElement ( 'script' ) ;
51
+ tag . id = "youtubeApiScript"
41
52
tag . src = 'https://www.youtube.com/iframe_api' ;
42
53
document . body . appendChild ( tag ) ;
54
+
43
55
}
44
56
45
57
loadVideo ( vidID : string ) {
0 commit comments