question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Computing this.player.getCurrentTime()

See original GitHub issue

Hi, thanks a lot for this wrapper for the Youtube API.

I’m having some trouble figuring out how to display a timestamp in my UI that updates as the video is playing (00:01, …), because using setTimeout doesn’t work due to this.player.getCurrentTime() being a Promise.

Any idea on how to do this?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
anteriovieiracommented, Apr 20, 2018

Hi @sergiocastrovale , try:

<youtube ... ref="youtube" @playing="playing" @paused="paused"/>

<span>{{ time }}</span> <span>{{ progress }} %</span>

export default {
  // ...
  data() {
    return {
      time: '00:00',
      progress: 0,
      processId: null,
    }
  },
  computed: {
    ply() {
      return this.$refs.video.youtube;
    },
  },
  methods: {
    async playing() {
      let totalTime = await this.ply.getDuration();

      this.processId = setInterval(() => {
        this.ply.getCurrentTime().then((time) => {
          let progress = (time / totalTime) * 100;

          this.progress = progress < 100 ? progress : 100;
          this.updateTime(time + 1);
        });
      }, 100);
    },

    updateTime(time) {
      time = Math.round(time);
      let minutes = Math.floor(time / 60);
      let seconds = time - minutes * 60;

      seconds = seconds < 10 ? '0' + seconds : seconds;
      minutes = minutes < 10 ? '0' + minutes : minutes;

      this.time = minutes + ':' + seconds;
    },

    pauseVideo() {
      this.ply.pauseVideo();
    },

    paused() {
      clearInterval(this.processId);
    }
  }
}
1reaction
sergiocastrovalecommented, Apr 20, 2018

@anteriovieira that worked perfectly. Thanks a lot!

Read more comments on GitHub >

github_iconTop Results From Across the Web

getCurrentTime() for YouTube Video
I'm writing a Chrome Extension in Javascript and I want to get the current time for the playing video on ...
Read more >
YouTube Player API Reference for iframe Embeds
This function loads the specified video's thumbnail and prepares the player to play the video. The player does not request the FLV until...
Read more >
Youtube Player API issue - help
I have a page on my app where i have the Youtube Player APi to see youtube ... Template.player.events({ 'click #play-btn': function() {...
Read more >
How to Control YouTube's Video Player with JavaScript
getCurrentTime() )); $('#duration').text(formatTime( player.getDuration() )); } function formatTime(time){ time = Math.round(time); var minutes ...
Read more >
Why does the currentTime seem off in my livestream & ...
To calculate the currentTime, THEOplayer follows the HTML5 specification. However, for livestreams, this is not specified. Hence, if you try to use currentTime...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found