javascript 获取嵌入式 iframe youtube 播放器的当前时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24646997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 03:09:41  来源:igfitidea点击:

Getting current time of embedded iframe youtube player?

javascriptjqueryyoutube-apiyoutube-javascript-api

提问by Siva G

Here i have tried as follows:

在这里,我尝试如下:

HTML

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.

在这里,我想要这样的操作,例如当播放器计时达到 6 秒时,我想更改 iframe 的 src。

回答by Jon B

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

该线程描述了如何使用 YouTube API获取当前 YouTube 视频时间来获取视频的当前播放时间

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.

然后可能使用 setInterval() 每秒运行一次以检查 API 中 getCurrentTime() 的值,如果 value = 6,则将 src 设置为新视频。

回答by Good Idea

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

这应该用 解决setInterval(),而不是setTimeout().

using the latest youtube iFrame API:

使用最新的 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);
}

回答by Rita Chavda

call below function when your video is starting playing

当您的视频开始播放时调用以下函数

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

function ChangeLinkUrl(){
var elem = document.getElementById('YourFrameID');
 elem.src = "newSorceURL";
}