使用YouTube API进行ontimeupdate [英] ontimeupdate with YouTube API

查看:112
本文介绍了使用YouTube API进行ontimeupdate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HTML5视频支持 ontimeupdate 事件; YouTube API中是否存在未在文档中列出的内容,或者只是我不知道的内容?我已经尝试了一个setInterval方法来检查每n毫秒,但是我必须检查视频是否正在播放等等。有没有人处理过这个问题?

HTML5 video supports the ontimeupdate event; is there an equivalent in the YouTube API that either isn't listed in the docs, or just that I'm unaware of? I've tried a setInterval method to check every n milliseconds, but then I have to check to see if the video is playing, etc. Has anyone dealt with this issue?

推荐答案

根据文档,没有这样的一个事件(我想你也知道这一点)。 其他答案也说明了这一点,就我所看到的代码示例而言,有没有直接的等价物即使在某些代码示例(查看入门代码)中,他们依赖在setTimeout上,为了在视频开始后6秒做一些事情。

According to the documentation there is no such an event (I think you are also aware of this). Other answers also state this and as far as I have seen over code examples, there is no direct equivalent. Even in some of the code examples (check the getting started code), they rely on setTimeout in order to do something 6 seconds after the video has started.

有些人找到了解决方法,例如这个答案,他们创建了一个计时器,这样他们实际上可以每隔X毫秒一次检查:

Some people have find work arounds, for example in this answer, they create a timer so they can actually fire every X milliseconds a time check:

function onPlayerReady() {
  function updateTime() {
    var oldTime = videotime;
    if(player && player.getCurrentTime) {
      videotime = player.getCurrentTime();
    }
    if(videotime !== oldTime) {
      onProgress(videotime);
    }
  }
  timeupdater = setInterval(updateTime, 100);
}

通过这种方式你可以实际模拟事件timeupdate,你只需要清除状态更改的超时(如果它已停止,您不想连续调用该函数)。

In this way you could actually simulate the event timeupdate, you just need also to clear the timeout on state change (if it is stopped you do not want to continuously call the function).

有些人创建了 YouTube播放器API包装器,如HTML5视频API 。在那个实际上你实际上是在嵌入式Youtube视频中访问HTML5视频API。

Some people have create a YouTube Player API wrapper like HTML5 video API. In that one you are actually accessing the HTML5 video API in the embedded Youtube Video.

我不确定Youtube API的路线图,但他们可能会转向一个更通用的标准API,与当前的HTML5 Video兼容,但在此之前我希望我的回答可以帮助你

I am not sure of the roadmap of the Youtube API, but probably they will move to a more generic and standard api that is compatible with the current HTML5 Video one, but until then I hope my answer can help you

这篇关于使用YouTube API进行ontimeupdate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆