YouTube API iFrame.seekTo () is not a method?

I know that .seekTo () works in the Javascript API, but I cannot get it to work with the iFrame API. Is this method supported? In the code below, the video successfully embeds and the log console, the player object, successfully completes. I get the error "player.seekTo is not a function" in the console.

<script> var tag = document.createElement('script'); tag.src = "http://www.youtube.com/player_api"; var firstScriptTag = document.getElementsByTagName('script')[0]; firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); var player; function onYouTubePlayerAPIReady() { player = new YT.Player('player', { height: '390', width: '640', videoId: 'u1zgFlCw8Aw' }); } $(window).load(function(){ jQuery('#test').click(function(){ //console.log('test'); console.log(player); player.seekTo(25); return false; }); }); </script> <a href="#" id="test">Test</a> 

Any ideas? Thanks!

+8
javascript php youtube youtube-api
source share
4 answers

Here's the updated jsfiddle using the new iframe api that works

FYI: if you only use inline HTML iframe code, you can put ?start=30 for start time

 <iframe width="640" height="390" src="//www.youtube.com/embed/p2H5YVfZVFw?start=30" frameborder="0" allowfullscreen></iframe> 

For the API, you can run the video at a specific time like this. Use the start parameter

 function onYouTubePlayerAPIReady() { player = new YT.Player('player', { height: '390', width: '640', videoId: 'p2H5YVfZVFw', playerVars: { 'start': 159, 'autoplay': 1, 'controls': 1 }, events: { 'onReady': onPlayerReady, 'onStateChange': onPlayerStateChange, } }); 

You can only call seekTo after the player has started playing or does nothing. Check the playerStateChange :

 onStateChange': onPlayerStateChange 

Add this callback function

 function onPlayerStateChange(evt) { if (evt.data==1) { // this will seek to a certain point when video starts // but you're better off using 'start' parameter // player.seekTo(22); } } 

jsFiddle has buttons below to search for 30, 60, 90 seconds. It has been tested with all browsers that rhyme with "rome". When it opens, a warning window appears for the player.seekTo function type. If it shows "undefined", you have a problem.

+6
source share

This works as intended: http://jsfiddle.net/JbwY4/ (FF 11, Chrome 17).

Ensure that the following conditions are met:

  • A container is identified with the identifier player : <div id="player"></div> .
  • The code is not delayed . That is, not in the onload , $(document).ready , setTimeout , etc. Otherwise, the IFrame API will not load and the code will not work.
  • loading jQuery.
+2
source share

The seekTo() method is only available after the onReady event.

Here is an example starting with 100 seconds:

http://jsfiddle.net/9RQK3/

+1
source share

I believe that not all javascript methods were implemented on the iframe api as an experimental one, so I don’t think that if you try it and it won’t work. However, I did not use iframe api in detail.

0
source share

All Articles