1

How can I get the currentTime of a YouTube video?

I have been searching in the YouTube api and I just do not understand: https://developers.google.com/youtube/js_api_reference?csw=1

I have been searching in Stack and try to apply what they say but it does not work. Problably I am missing something that I do not understand:
Youtube API returning current time
Getting Current YouTube Video Time

I would appreciate any guidence to know where to begin

You can check yourself: http://jsfiddle.net/twL9z8e1/

$("#bookmarkBoto").click(function() {
  
    //var time = $("#yt").currentTime();//no
 //var time = $("#yt").getCurrentTime();//no  
    $('#check').text(time);
   
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<iframe id="yt" name="yt" width="720" height="405" src="https://www.youtube.com/embed/M7lc1UVf-VE" frameborder="0" allowfullscreen></iframe>

<br><br>

<div id="bookmarkBoto">bookmarkBoto</div>
<div id="check">check</div>
Community
  • 1
  • 1
segon
  • 273
  • 1
  • 4
  • 16

1 Answers1

0

No sample way, you need to use the YouTube API. This is the javascript code. I made a live example at the end.

JAVASCRIPT

var tag = document.createElement('script');

tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

// 3. This function creates an <iframe> (and YouTube player)
//    after the API code downloads.
var player;

function onYouTubeIframeAPIReady() {
    player = new YT.Player('player', {
        events: {
            'onReady': onPlayerReady
        }
    });
}

function onPlayerReady(event) {
    event.target.playVideo();
}

$('button').click(function() {
  console.log(player.getCurrentTime())
})

HTML

<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-2.1.1.min.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
  <iframe id="player" width="640" height="360" src="https://www.youtube.com/embed/zua0_IXcPFY?enablejsapi=1" frameborder="0" allowfullscreen></iframe>

  <button>click time</button>

</body>
</html>

LIVE DEMO

mpgn
  • 7,121
  • 9
  • 67
  • 100
  • Sorry, I amb a beginner. I just do not understand your code. I was asking click a button and get the currentTime of the video in a var and with jQuery – segon Feb 28 '15 at 15:48
  • it works in js bin but if I copy-paste and put in my server it does not work. In console I have this error: [Error] Blocked a frame with origin "https://www.youtube.com" from accessing a frame with origin "http://www.myDomain". The frame requesting access has a protocol of "https", the frame being accessed has a protocol of "http". Protocols must match. (anonymous function) (html5player.js, line 828) global code (html5player.js, line 1688) I tried what I see here: and this does not solve the error and cannot see the console.log currentTime – segon Mar 01 '15 at 12:02
  • @segon remove the s from https in the iframe. like `src="http://www.youtube.com/embed/zua0_IXcPFY?enablejsapi=1"` – mpgn Mar 01 '15 at 13:44
  • I remove the s from https in the iframe and I have the same message in the console – Nrc Mar 03 '15 at 14:47
  • add the option origin in http://www.youtube.com/embed/zua0_IXcPFY?enablejsapi=1&origin=http://yoururl. Add a s to http – mpgn Mar 03 '15 at 16:35