11

It is possible to link to a specific time in a YouTube video appending the #t=1m49s syntax.

Is it possible to have links on the same page of the embed that make the video jump to different times in the video?

<a href="">Link to 1 minutes 10 seconds</a>
<a href="">Link to 3 minutes 4 seconds</a>
<a href="">Link to 5 minutes 10 seconds</a>
etc..
bookcasey
  • 39,223
  • 13
  • 77
  • 94

3 Answers3

9

Goran has posted the api reference. I would recommend checking that out. Here is some basic code for what you're looking for though. I've commented the main parts:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js"></script>    
<script>
    //this function gets called when the player is ready    
    function onYouTubePlayerReady (playerId) {
        ytplayer = document.getElementById("myytplayer");
        console.log(ytplayer);
    }
    //generic seekTo function taking a player element and seconds as parameters    
    function playerSeekTo(player, seconds) {
        player.seekTo(seconds);
    }
</script>

<div id="ytapiplayer">
  You need Flash player 8+ and JavaScript enabled to view this video.
</div>
<br/>
<a href="#" onclick="playerSeekTo(ytplayer, 70); return false;">Link to 1 minutes 10 seconds</a>
<a href="#" onclick="playerSeekTo(ytplayer, 90); return false;">Link to 1 minutes 30seconds</a>
<a href="#" onclick="playerSeekTo(ytplayer, 110); return false;">Link to 1 minutes 50 seconds</a>

Now the js:

var ytplayer;

$(document).ready(function() {
    swfobject.embedSWF("//www.youtube.com/e/Py_IndUbcxc?enablejsapi=1&playerapiid=ytplayer &version=3",
    "ytapiplayer", // where the embedded player ends up
    "425", // width    
    "356", // height    
    "8", // swf version    
    null,
    null, {
        allowScriptAccess: "always"
    }, {
        id: "myytplayer" // here is where the id of the element is set
    });

});

Here is the fiddle that I created.

sinemetu1
  • 1,726
  • 1
  • 13
  • 24
  • Thanks for the help! However, I can't seem to get it to work. Could you perhaps provide a Jsfiddle? Thank you! – bookcasey Feb 08 '12 at 23:52
  • 1
    @bookcasey I've updated the code in the original answer and included a working fiddle. Let me know if you have any questions. – sinemetu1 Feb 09 '12 at 00:47
  • 1
    Those who uses Firefox, the fiddle is not broken, only that displays the video when view result in separate window. – lulalala Mar 18 '13 at 13:07
4

Although I have no personal experience in implementing javascript with emdedded YouTube videos, I do know that YouTube have a JavaScript API to perform such actions. The function you are looking for is .seekTo()

http://code.google.com/apis/youtube/js_api_reference.html#seekTo

With a nifty little demo here.

Goran Mottram
  • 6,244
  • 26
  • 41
  • If you want to use the iframe embed instead of the Flash embed, the documentation for that is at https://developers.google.com/youtube/iframe_api_reference – DavGarcia Dec 03 '14 at 20:38
1

Use ?start=xyz (seconds)

For example:

<iframe src="http://www.youtube.com/embed/vDFuzhnTegc?start=1778" width="420" height="235"></iframe>
Andrew
  • 4,953
  • 15
  • 40
  • 58
Nykyta
  • 19
  • 1