smnbbrv / ngx-plyr

Angular 6+ binding for Plyr video & audio player
https://smnbbrv.github.io/ngx-plyr/
MIT License
100 stars 65 forks source link

currentTime getter always returns 0 #62

Open jhayg12 opened 3 years ago

jhayg12 commented 3 years ago

Hi there,

I'm trying to get the currentTime value of the video that was currently playing on pause event but it always returns 0.

@ViewChild(PlyrComponent) plyr: PlyrComponent;

videoPlayer: Plyr;

pause(event: Plyr.PlyrEvent) {
  console.log(this.videoPlayer.currentTime);
}
KangHidro commented 3 years ago

You may try:

@ViewChild(PlyrComponent, { static: true }) plyr: PlyrComponent;
// videoPlayer: Plyr; // is not needed
...
this.plyr.player.currentTime
...
freitas-patrick commented 3 years ago

I had the same issue, and guess that in some moment's the player isn't avaliable, therefore the currentTime is always 0.

I realized that has two methods that are executed in moments where the currentTime property are filled, are they:

// on html
<plyr (plyrInit)="initPlyr($event)" (plyrPlay)="played($event)"></plyr>
// on typescript
@ViewChild(PlyrComponent)
plyr!: PlyrComponent;
player!: Plyr;

var _this = this;
initPlyr(event: any) {
   this.player.on('timeupdate', (event) => {
      console.log(_this.player.currentTime);
   }
}

played(event: Plyr.PlyrEvent) {
    console.log(this.player.currentTime);
}