Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Youtube API Set current time

I am currently using the You-Tube API for a project of mine. The way I use it is the following:

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;
var alternatePlayer;
function onYouTubeIframeAPIReady() {
    player = new YT.Player('player', {
        height: '390',
        width: '640',
        videoId: 'M7lc1UVf-VE',
        events: {
            'onReady': onPlayerReady
        }
    });

}

// 4. The API will call this function when the video player is ready.
function onPlayerReady(event) {
    event.target.playVideo();
}

It all works well, but for my project I need to be able to control the current time that the video is playing. I have searched the You-Tube API notes and data but could not find a paramter nor a function that enables me to set the current time of the video (There is a GET for the current time but not a set). There is a parameter startseconds that when mentioned in the youtube creation (I mean near the parameters height,width,videoId) can control what second the video starts from. This does not fit my needs because I need to change the current time of the SAME video and not load another one. Is there any way around this? Am I missing this set function? Must I recall the same videoId with the startseconds? Thanks very much in advance for any light on that matter.


EDIT: Okay, I have found the solution in some weird way. For some reason I don't see it mentioned on the You-Tube API.

player.seekTo(55,true); 

This code will change the you-tube player to 55 seconds, which is a SET for the current time. Thanks everyone for your time.

like image 265
Oranges Avatar asked May 14 '14 10:05

Oranges


1 Answers

Take a look into the following documents:

  • https://developers.google.com/youtube/js_api_reference
  • https://developers.google.com/youtube/youtube_player_demo

First you need a JS reference to the Player object. For a quick start, please take a look at the following Question:

  • How do I get the reference to an existing YouTube player?

With this reference you can do:

player.seekTo(42); //Go to 42 seconds
like image 142
Christian Gollhardt Avatar answered Sep 21 '22 23:09

Christian Gollhardt