Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Youtube API - getAvailableQualityLevels return an empty array

Tags:

youtube-api

i get an empty array when i call the youtube getAvailableQualityLevels() function, but why?
The video has many quality-levels like 360p 480p ... 720p

DEMO: http://jsbin.com/eyexah/1/edit

YT API Doc 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>
like image 501
user1815934 Avatar asked Nov 11 '12 11:11

user1815934


1 Answers

This would work if the state would be playing (or 1).

add a listener for onStateChange events in your onYouTubePlayerReady function:

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

and add the onPlayerStateChange function, for example:

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

See a working example based on your code: http://jsbin.com/eyexah/6/edit

like image 157
CD.. Avatar answered Oct 19 '22 04:10

CD..