bookcasey
bookcasey

Reputation: 40511

Links to Specific Times in YouTube iframe embed

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..

Upvotes: 11

Views: 8964

Answers (3)

Nykyta
Nykyta

Reputation: 19

Use ?start=xyz (seconds)

For example:

<iframe src="http://www.youtube.com/embed/vDFuzhnTegc?start=1778" width="420" height="235"></iframe>

Upvotes: 1

sinemetu1
sinemetu1

Reputation: 1726

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.

Upvotes: 9

Goran Mottram
Goran Mottram

Reputation: 6304

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.

Upvotes: 4

Related Questions