Youtube API - getAvailableQualityLevels返回一个空数组

时间:2012-11-11 11:08:33

标签: youtube-api

当我调用youtube getAvailableQualityLevels()函数时,我得到一个空数组,但为什么呢? 该视频有许多质量等级,如360p 480p ... 720p

样本: http://jsbin.com/eyexah/1/edit

YT API文档 https://developers.google.com/youtube/js_api_reference#GettingReference

CODE:

<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js" type="text/javascript"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
<script type="text/javascript" language="javascript">
      var params = { allowScriptAccess: "always" };
      var atts = { id: "youtube_player" };
      swfobject.embedSWF('http://www.youtube.com/v/ZGx2WUbhnyM?enablejsapi=1&playerapiid=ytplayer&version=3',
                         'youtube',
                         '425',
                         '356',
                         '8',
                         null,
                         null,
                         params,
                         atts);

</script>
<script type="text/javascript" language="javascript">
function onYouTubePlayerReady()
{
    console.log('# onYouTubePlayerReady');
    var ytp = document.getElementById("youtube_player");
    var getQuali = ytp.getAvailableQualityLevels();
    console.log(getQuali);
};
</script>
</head>

<body>       
  <div id="youtube"></div>
</body>
</html>

2 个答案:

答案 0 :(得分:8)

如果状态为playing(或1),这将有效。

onStateChange函数中添加onYouTubePlayerReady个事件的监听器:

ytp.addEventListener('onStateChange', 'onPlayerStateChange');

并添加onPlayerStateChange函数,例如:

function onPlayerStateChange(event)
{
   var getQuali = ytp.getAvailableQualityLevels();
}

根据您的代码查看工作示例:http://jsbin.com/eyexah/6/edit

答案 1 :(得分:0)

您可以在状态“正在播放”时检索 检查onPlayerStateChange(事件)