Youtube player JS API seekTo function not working

倖福魔咒の 提交于 2019-12-01 16:14:15

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.

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.

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);
        }
    }
}

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!