JS API function for Youtube does not work

I am trying to use the colorless Youtube player through the JS API and I am having problems with the seekto () function.

The problem is that sometimes (I can’t say when the problem occurs), calling the seekTo () function back to the point in the unloaded video does not return and instead remains at the current time.

This is what I do (imagine a video lasts 240 seconds):

  • I am launching a video -> loadVideoById ().
  • I move forward (let’s say) to the middle of the video β†’ seekTo (120)
  • The video automatically goes to the position that I set.
  • I try to go back up to 25% of the time of the video β†’ seekTo (60)
  • Sometimes a video returns to 60, but more often it only returns to 120.

So, does anyone else have this problem?

+8
javascript youtube-javascript-api
source share
4 answers

Posting some code will help us help you, but you tried player.seekTo(60, true); - the second parameter is important - setting it to true will cause the player to send a new request to the server for video.

+4
source share

I had the same problem that you described. I found out that if you request an API for the duration of the video, and after that look for the return location, then it seems to work.

For example, this is my test:

 var duration = ytplayer.getDuration(); ytplayer.seekTo(0, true); 

He showed me that he always returned to his starting position.

0
source share

I have the same problem, but none of these solutions worked for me. I ended up using this to make it work with viewing HTML5 (Chrome and Firefox).

 function onPlayerStateChange(event) { if (event.data == YT.PlayerState.PLAYING){ // for Chrome and Firefox to "restart" properly var ct = player.getCurrentTime(); var dur = player.getDuration(); if (ct > (dur-.5) ){ player.seekTo(0, true); } } } 
0
source share

Be careful, the document says:

"The player will advance to the nearest keyframe before this time , if the player has not already uploaded the part of the video that the user is looking for . In this case, the player will go to the nearest keyframe before or after the specified time in accordance with the seek () method of the NetStream Flash Player object . (See the Adobe documentation for more information.)

https://developers.google.com/youtube/js_api_reference?hl=fr#seekTo

0
source share

All Articles