Getting Current YouTube Video Time

后端 未结 7 791
[愿得一人]
[愿得一人] 2020-11-28 08:27

I am writing a Browser Plugin and need to find a way to get the current time a YouTube Video playing on YouTube using JavaScript. I have been playing around in the Chrome Ja

相关标签:
7条回答
  • 2020-11-28 08:58
    ytplayer = document.getElementById("movie_player");
    ytplayer.getCurrentTime();
    

    See the api

    0 讨论(0)
  • 2020-11-28 08:59

    Depends on what you want

    player.getCurrentTime():Number
    

    Returns the elapsed time in seconds since the video started playing.

    player.getDuration():Number
    

    Returns the duration in seconds of the currently playing video. Note that getDuration() will return 0 until the video's metadata is loaded, which normally happens just after the video starts playing.

    http://code.google.com/apis/youtube/js_api_reference.html

    0 讨论(0)
  • 2020-11-28 09:13

    You can use Html5 Video API on youtube.com

    var htmlVideoPlayer = document.getElementsByTagName('video')[0];
    htmlVideoPlayer.currentTime
    

    Note: It's not gonna work on Youtube Iframe API because Iframes are isolated. You cannot access the context of a Youtube IFrame .

    0 讨论(0)
  • 2020-11-28 09:14

    In 2020, this works:

    player.playerInfo.currentTime
    

    full code: see it live on codepen

    0 讨论(0)
  • 2020-11-28 09:15

    Just FYI, There is a new iframe API for the YouTube player: https://developers.google.com/youtube/iframe_api_reference

    0 讨论(0)
  • 2020-11-28 09:17

    Stop at specific time youtube video and show notification box in GWD

    <script>
      var yid = document.getElementById("gwd-youtube_1");
      var idBox = document.getElementById("box1");
    pausing_function = function(event) { 
    
        var aa = setInterval(function() {
    
            if (yid.getCurrentTime() > 8.0 && yid.getCurrentTime() < 8.1) {
                yid.pause(yid);
                idBox.style.opacity = 1;
                console.log(yid.getCurrentTime() + "playing")
    
                 clearInterval(aa);
                yid.removeEventListener("playing", pausing_function);
    
            }
    
        }, 100)
    }
    
     yid.addEventListener("playing", pausing_function);
    
      var pausing_function_1 = function() {      
           if (yid.getCurrentTime() > 8.1) {
            console.log(yid.getCurrentTime() + "pause")
    
    
            // remove the event listener after you paused the playback
            yid.removeEventListener("playing", pausing_function);
          }
        };    
      </script>
    

    play video and hide notification

     <script type="text/javascript" gwd-events="handlers">
        window.gwd = window.gwd || {};
        gwd.pauseVideo = function(event) {
          var idBox = document.getElementById("box1");
          idBox.style.opacity = 0;
        };
      </script>
      <script type="text/javascript" gwd-events="registration">
        // Support code for event handling in Google Web Designer
         // This script block is auto-generated. Please do not edit!
        gwd.actions.events.registerEventHandlers = function(event) {
          gwd.actions.events.addHandler('gwd-youtube_1', 'playing', gwd.pauseVideo, false);
        };
        gwd.actions.events.deregisterEventHandlers = function(event) {
          gwd.actions.events.removeHandler('gwd-youtube_1', 'playing', gwd.pauseVideo, false);
        };
        document.addEventListener("DOMContentLoaded", gwd.actions.events.registerEventHandlers);
        document.addEventListener("unload", gwd.actions.events.deregisterEventHandlers);
      </script>
    
    0 讨论(0)
提交回复
热议问题