11

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

演示:http: //jsbin.com/eyexah/1/edit

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

代码:

<!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>
4

2 回答 2

8

playing如果状态是(或1),这将起作用。

onStateChange在函数中添加事件监听器onYouTubePlayerReady

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

并添加onPlayerStateChange功能,例如:

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

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

于 2012-11-14T07:28:03.217 回答
0

您可以检索状态何时为“正在播放”。
检查 onPlayerStateChange(event)

于 2012-11-12T03:50:32.700 回答