Youtube player JS API seekTo function not working

I'm trying to use Youtube chromeless player through the JS API and I'm having problems with seekto() function.

The problem is that sometimes (I can't tell when the problem arises), calling seekTo() function backwards to a point in the video that is not loaded don't come back and instead stays in the current time.

This is what I do (imagine the video is 240 seconds length):

  1. I start playing a video -> loadVideoById().
  2. I move forward to (let's say) the middle of the video -> seekTo(120)
  3. Video jumps correctly to the position I asked.
  4. I try to move backwards to 25% time of the video -> seekTo(60)
  5. Sometimes the video jumps back to 60 but more often it only goes back to 120.

So, is anyone else having this issue?

Answers:

Answer

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

Answer

I had the same problem you have described. What I found out is that if you query the API for the video duration and after that seek to the backward location then it seems to work.

For example, this is my test:

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

It showed me that always returned to the initial position.

Answer

I have the same problem but none of these solutions worked for me. I ended up using this to get it working with the HTML5 viewer (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);
        }
    }
}
Answer

Be carefull, in the doc it says :

"The player will advance to the closest keyframe before that time unless the player has already downloaded the portion of the video to which the user is seeking. In that case, the player will advance to the closest keyframe before or after the specified time as dictated by the seek() method of the Flash player's NetStream object. (See Adobe's documentation for more information.)"

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

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.