如何检测youtube getCurrentTime()中的更改? [英] How to detect change in youtube getCurrentTime()?

查看:234
本文介绍了如何检测youtube getCurrentTime()中的更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以检测 YouTube API

目的:要在我的页面中播放有限时间(<1分钟)的单个视频,然后转到下一个视频。

Purpose:To play single video for limited time(<1 min) in my page and move on to next video.

我想使用object.watch,但它仅适用于变量,而不适用于函数。

I was thinking to use object.watch but it only works for the variables not the functions.

我还尝试将某些内容绑定到原始源代码 https:// s.ytimg.com/yts/jsbin/www-widgetapi-vfl9twtxR.js ,但是太复杂了。

I also try to bind something in the original source code https://s.ytimg.com/yts/jsbin/www-widgetapi-vfl9twtxR.js but it was too complicated.

推荐答案

由于YouTube播放器包装在iFrame中,因此您需要依赖播放器显示的内容,尽管他们通过getCurrentTime()函数显示了当前时间,但他们并没有公开任何可能出现的事件更新(实际上,时间仅每秒更新为公开函数6或7次,并且并不总是一致的。)。

Because the YouTube player is wrapped in an iFrame, you are dependent on what the player exposes, and while they've exposed the current time via the getCurrentTime() function, they haven't exposed any events raised whenever the time might get updated (in fact, the time is only updated to the exposed function 6 or 7 times per second, and it isn't always consistent).

因此,您唯一的选择是设置一个JavaScript计时器。有很多方法可以做到这一点;一个简单的例子就是这样:

So your only option is to set up a javascript timer. Lots of ways to do that; a simple one would be like this:

setInterval(function(){
   // here you'd raise some sort of event based on the value of getCurrentTime();
   },100); // polling 8 times a second, to make sure you get it every time it changes. 

由于来自iFrame的postMessage并不总是完全一致,因此您可以使此间隔计时器更大。或者,您可以使用诸如requestAnimationFrame之类的工具每秒轮询60次。

As the postMessage from the iFrame isn't always perfectly consistent, you could have this interval timer be greater. Or you could use something like requestAnimationFrame to poll 60 times a second.

这篇关于如何检测youtube getCurrentTime()中的更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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