How to pause youtube-player via typescript?

I am making an application in Angular (9.11) and I need to know how long the user has watched a video on youtube. I am using the YouTube-player component of angular. I monitor in the component itself the change of State. My problem is when the user leaves the page without having paused the video as the state Change event is not fired. How can I get in ngOnDestroy the time of the video or send a command to the video pause?

<div class="col-12 text-center p-3">
  <youtube-player
    id="player"
    videoId="{{content?.content}}"
    (stateChange)="playerEvent($event)"
    width="712px"
    height="400px"
  ></youtube-player>
</div>
Author: Daniel Girardi, 2021-01-14

1 answers

Figured out the trick, save the event that is fired when the video gets ready to play:

 <youtube-player
    videoId="{{content?.content}}"
    (stateChange)="playerEvent($event)"
    (ready)="savePlayer($event)"
    width="712px"
    height="400px"
  ></youtube-player>

And in typescript:

savePlayer(event) {
    this.player = event;
}

The variable this.player will have everything you need to command the video by script.

 0
Author: Daniel Girardi, 2021-01-14 17:27:13