11

Here i have tried as follows:

HTML

<iframe id="player" style="position: relative; height: 220px; width: 400px" src="http://www.youtube.com/embed/7a_CVFRqYv4?rel=0&amp;enablejsapi=1"></iframe>

Script

var player;
function onYouTubePlayerAPIReady() {player = new YT.Player('player');}

Here i want to such action like when the player timing reaches 6 seconds,i want to change the src of the iframe.

Siva G
  • 1,170
  • 3
  • 17
  • 35

3 Answers3

7

This thread describes how you can get the current playing time of a video using the YouTube API Getting Current YouTube Video Time

Then maybe use setInterval() to run every second to check the value of getCurrentTime() from the API and if value = 6 set the src to the new video.

Community
  • 1
  • 1
Jon B
  • 2,444
  • 2
  • 18
  • 19
  • i don't want to use the setTimeout() function buddy,because the person,who seek the video into starting then also it should respond the same process. – Siva G Jul 09 '14 at 06:57
  • So you don't want to respond to a specific time in the video eg. at the 6s mark change src to x? The timeout would only be used to periodically check the value of ytplayer.getCurrentTime(); – Jon B Jul 09 '14 at 07:01
1

this should be solved with setInterval(), not setTimeout().

using the latest youtube iFrame API:

var player;
var checkInt; // save this as a var in this scope so we can clear it later
function onYouTubePlayerAPIReady() {
   player = new YT.Player('player');
   startInterval()
}

function startInterval() {
   //checks every 100ms to see if the video has reached 6s
   checkInt = setInterval(function() {
      if (player.getCurrentTime() > 6) {
         changeSrc();
         clearInterval(checkInt);
      };
   }, 100)
}

function changeSrc() {
   player.loadVideoById(xxxxx);
}
Good Idea
  • 2,481
  • 3
  • 18
  • 25
-3

call below function when your video is starting playing

function StartVideo(){
setTimeout("ChangeLinkUrl()", 6000);
}

function ChangeLinkUrl(){
var elem = document.getElementById('YourFrameID');
 elem.src = "newSorceURL";
}
Rita Chavda
  • 191
  • 2
  • 16