Link to specific time in a youtube video via seekT

2019-09-02 07:45发布

问题:

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.