YouTube API 中的 getPlayList() 返回空数组 [英] getPlayList() in YouTube API returning empty array

查看:15
本文介绍了YouTube API 中的 getPlayList() 返回空数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,我希望能够在其中 console.log out 播放列表中的视频列表:

I have the following code, where I hoped to be able to console.log out the list of videos in the playlist:

function onYouTubePlayerReady(playerId) {
        ytplayer = document.getElementById("myytplayer");

        ytplayer.cuePlaylist({
            list:"PLf71xE2jRgTXB_LeUJkXxFwCc4r1z5if3"
        });
        console.log(ytplayer.getPlaylist());
}

但是 getPlayList() 方法只返回一个空数组.

But the getPlayList() method just returns an empty array.

当我从这个函数外部调用 getPlaylist() 时,它会返回正确的数组.为什么这个方法在这里返回一个空数组?以及如何获取播放列表数组?

When playing around with it if I call getPlaylist() from outside of this function it does return the correct array. Why is this method returning an empty array here? And how can I get the playlist array?

非常感谢.

推荐答案

我自己刚刚偶然发现了这个问题.这个问题似乎是播放器的一个怪癖,以及 YouTube(谷歌)如何定义就绪"

Just stumbled across this problem myself. The issue seems to be a quirk with the player, and how YouTube (google) have defined "ready"

当播放器的chrome"完成渲染(即它绑定到 HTML 文档)时,播放器的 onReady 函数似乎会触发.实际的播放器数据(视频或播放列表)是异步加载的,因此可以在之后的任意时间点准备就绪.

It seems that the onReady function of the player fires when the "chrome" for the player has finished rendering (i.e. it's tied the HTML document). The actual player data (video or playlist) is loaded asynchronously, so is ready at some arbitrary point after.

我制定了一个简单的解决方法来查看玩家的状态变化事件

I put a simple work-around in place that looks at the state-change event of the player

var firstLoad = true;
function onPlayerStateChange(e) {
  if (firstLoad && e.data == YT.PlayerState.CUED) {
      //First Cued event - playlist is now available
      firstLoad = false
      myPlaylistFunction(player.getPlaylist());
  }
}

这篇关于YouTube API 中的 getPlayList() 返回空数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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