问题 指定时间内的Youtube API事件


是否可以通过YouTube API在视频达到指定时间时触发事件?例如当视频达到20秒。解雇事件。

谢谢,
毛罗


2502
2018-04-16 13:47


起源



答案:


不确定你是否仍然需要一个答案(因为我在4个月后发现它)但是这是我用youtube的iframe嵌入api完成这个的方法。它的丑陋之处在于它需要一个setInterval,但YouTube API中确实没有任何“timeupdate”事件(至少在iframe API中没有),所以你必须通过每次检查视频时间来伪造它很经常。它似乎运行得很好。

假设您想要启动播放器 如图所示 使用YT.Player(),你想实现自己的 onProgress() 每当视频时间改变时调用的函数:

在HTML中:

<div id="myPlayer"></div>

在JS中:

// first, load the YouTube Iframe API:
var tag = document.createElement('script');
tag.src = "//www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

// some variables (global here, but they don't have to be)
var player;
var videoId = 'SomeYoutubeIdHere';
var videotime = 0;
var timeupdater = null;

// load your player when the API becomes ready
function onYoutubeIframeAPIReady() {
  player = new YT.Player('myPlayer', {
    width: '640',
    height: '390',
    videoId: videoId,
    events: {
      'onReady': onPlayerReady
    }
  });
}

// when the player is ready, start checking the current time every 100 ms.
function onPlayerReady() {
  function updateTime() {
    var oldTime = videotime;
    if(player && player.getCurrentTime) {
      videotime = player.getCurrentTime();
    }
    if(videotime !== oldTime) {
      onProgress(videotime);
    }
  }
  timeupdater = setInterval(updateTime, 100);
}

// when the time changes, this will be called.
function onProgress(currentTime) {
  if(currentTime > 20) {
    console.log("the video reached 20 seconds!");
  }
}

这有点草率,需要一些全局变量,但你可以很容易地将它重构为一个闭包和/或使间隔停止并在播放/暂停时开始,同时在初始化播放器时包括onStateChange事件,并编写onPlayerStateChange检查播放和暂停的功能。您只需要从onPlayerReady中分离updateTime()函数,然后进行策略调用 timeupdater = setInterval(updateTime, 100); 和 clearInterval(timeupdater); 在正确的事件处理程序中。 看这里 有关在YouTube上使用活动的更多信息。


13
2017-08-02 16:49





我想你可以看看 popcorn.js。 这是一个有趣的mozilla项目,似乎可以解决您的问题。


1
2018-01-25 11:24



Mozilla不再维护这一点 - Gibolt


答案:


不确定你是否仍然需要一个答案(因为我在4个月后发现它)但是这是我用youtube的iframe嵌入api完成这个的方法。它的丑陋之处在于它需要一个setInterval,但YouTube API中确实没有任何“timeupdate”事件(至少在iframe API中没有),所以你必须通过每次检查视频时间来伪造它很经常。它似乎运行得很好。

假设您想要启动播放器 如图所示 使用YT.Player(),你想实现自己的 onProgress() 每当视频时间改变时调用的函数:

在HTML中:

<div id="myPlayer"></div>

在JS中:

// first, load the YouTube Iframe API:
var tag = document.createElement('script');
tag.src = "//www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

// some variables (global here, but they don't have to be)
var player;
var videoId = 'SomeYoutubeIdHere';
var videotime = 0;
var timeupdater = null;

// load your player when the API becomes ready
function onYoutubeIframeAPIReady() {
  player = new YT.Player('myPlayer', {
    width: '640',
    height: '390',
    videoId: videoId,
    events: {
      'onReady': onPlayerReady
    }
  });
}

// when the player is ready, start checking the current time every 100 ms.
function onPlayerReady() {
  function updateTime() {
    var oldTime = videotime;
    if(player && player.getCurrentTime) {
      videotime = player.getCurrentTime();
    }
    if(videotime !== oldTime) {
      onProgress(videotime);
    }
  }
  timeupdater = setInterval(updateTime, 100);
}

// when the time changes, this will be called.
function onProgress(currentTime) {
  if(currentTime > 20) {
    console.log("the video reached 20 seconds!");
  }
}

这有点草率,需要一些全局变量,但你可以很容易地将它重构为一个闭包和/或使间隔停止并在播放/暂停时开始,同时在初始化播放器时包括onStateChange事件,并编写onPlayerStateChange检查播放和暂停的功能。您只需要从onPlayerReady中分离updateTime()函数,然后进行策略调用 timeupdater = setInterval(updateTime, 100); 和 clearInterval(timeupdater); 在正确的事件处理程序中。 看这里 有关在YouTube上使用活动的更多信息。


13
2017-08-02 16:49





我想你可以看看 popcorn.js。 这是一个有趣的mozilla项目,似乎可以解决您的问题。


1
2018-01-25 11:24



Mozilla不再维护这一点 - Gibolt


不确定是否有人在此同意,但我可能更喜欢使用setTimeout()而不是setInterval(),因为它避免使用事件侦听器,例如:

function checkTime(){
    setTimeout(function(){
        videotime = getVideoTime();
        if(videotime !== requiredTime) {
             checkTime();//Recursive call.
        }else{
            //run stuff you want to at a particular time.
            //do not call checkTime() again.
        }
    },100);
}

它是一个基本的伪代码,但希望传达这个想法。


0
2017-12-11 22:19