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.
Take a look into the following documents:
First you need a JS reference to the Player
object. For a quick start, please take a look at the following Question:
With this reference you can do:
player.seekTo(42); //Go to 42 seconds
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With