YouTube Player API:getDuration(),getCurrentTime(),getVideoData()不起作用 [英] YouTube Player API: getDuration(), getCurrentTime(), getVideoData() not working

查看:93
本文介绍了YouTube Player API:getDuration(),getCurrentTime(),getVideoData()不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的应用程序,我正在尝试使用YouTube Iframe播放器API播放视频,并允许用户无需重新加载页面即可更改视频.

For my application, I am trying to use the YouTube Iframe player API to play videos, and allow the user to change videos without reloading the page.

我通过使用 player.loadVideoById(video_id)方法完成此操作.通过YouTube视频ID加载视频后,我还想捕获有关该视频的所有相关信息.但是,我注意到没有任何get函数可以立即使用,包括 getDuration() getCurrentTime() getVideoData().

I accomplish this by using the player.loadVideoById(video_id) method. After loading the video via YouTube video id, I also want to capture all relevant information about the video. However, I notice that none of the get functions work right away, including getDuration(), getCurrentTime() and getVideoData().

我发现了一种使用 setTimout 来获取使用 player.loadVideoById(video_id)后大约1-2秒的信息的解决方案,但是,我没有认为这是正确的解决方案.

I have found a scrappy solution of using a setTimout to get the information about 1-2 seconds after using player.loadVideoById(video_id), however, I do not think this is the proper solution.

在我看来, loadVideoById(video_id)正在从YouTube异步获取数据.是否有可用的回调函数?我在文档中找不到一个.请帮忙.

It seems to me that loadVideoById(video_id) is asynchronously fetching the data from YouTube. Is there a callback function available to use? I could not find one in the docs. Please help.

谢谢!

推荐答案

有很多

There are quite a few Events available that you can utilise I think.

这是一个跟踪 YT.PlayerState.PLAYING 状态的示例,该状态属于 onStateChange 事件,该事件基本上是在说明当前正在播放视频.

Here is an example of tracking YT.PlayerState.PLAYING state, falling within the onStateChange event, which is basically saying that the video is currently being played.

player.addEventListener(YT.PlayerState.PLAYING, onVideoPlaying, false);

function onVideoPlaying() {
  console.log('currentTime:', player.getCurrentTime(), 'duration:', player.getDuration());
}

希望这会有所帮助.

这篇关于YouTube Player API:getDuration(),getCurrentTime(),getVideoData()不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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