Link to specific time in a youtube video via seekTo();

我的梦境 提交于 2019-12-12 01:49:21

问题


I’m trying to get something like an index page of a youtube video so that there are links to specific times of the video. I tried it to do like explained in Links to Specific Times in YouTube iframe embed, but somehow it doesnt seem to work. This is my code:

<script>
    var tag = document.createElement('script');
    tag.src = "//www.youtube.com/player_api";
    var firstScriptTag = document.getElementsByTagName('script')[0];
    firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

    var player;
    function onYouTubePlayerAPIReady() {
        player = new YT.Player('video', { events: {'onReady': onPlayerReady}});
    }

    function playerSeekTo(seconds) {
        player.seekTo(seconds);
    }
</script>

HTML:

<iframe src="https://www.youtube.com/embed/FKWwdQu6_ok?enablejsapi=1" frameborder="0" allowfullscreen id="video">
</iframe>

<a href="#" onclick="playerSeekTo(70); return false;">Link to 1 minutes 10 seconds</a>
<a href="#" onclick="playerSeekTo(90); return false;">Link to 1 minutes 30seconds</a>
<a href="#" onclick="playerSeekTo(110); return false;">Link to 1 minutes 50 seconds</a>

回答1:


I encountered three problems:

1st

tag.src = "//www.youtube.com/player_api";

led to an error. Should be

tag.src = "http://www.youtube.com/player_api";

2nd

player = new YT.Player('video', { events: {'onReady': onPlayerReady}});

Obviously, onPlayerReady is not defined. When you remove this part, it'll work fine. What do you want to achieve with this?

player = new YT.Player('video');

(3rd)

To me, the video you embed is not available. When I embed another video (e.g. Im69kzhpR3I), everything behaves as it should.



来源:https://stackoverflow.com/questions/27641808/link-to-specific-time-in-a-youtube-video-via-seekto

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