Javascript / youtube api check state for playback restrictions


Javascript / youtube api validation state for playback restrictions


The problem is I am embedding from gdata / youtube-api and sometimes it returns playback limiting video or embedding is not allowed. So I checked the status or something to deal with it. I tried adding & format = 5. and tried -1 event. No luck. Any ideas are greatly appreciated. function onPlayerStateChange (event) {if (event.data == -1) {// rest of the code}} Thanks in advance.

Edit3 / Solution to this problem: Since you had onError set for a function in that function, capturing event and error checking was the solution to this problem. function onPlayerError (event) {if (event.data === 150 || event.data === 101) {// rest here}}

EDIT: adding more code. EDIT 2: matching error labels is the answer, the only problem is I don't know how to capture just the error code any ideas?

<iframe id="player" type="text/html" width="560" height="50" src="'.$striphttp.'"></iframe>



  var tag = document.createElement('script');
  tag.src = "http://www.youtube.com/player_api";
  var firstScriptTag = document.getElementsByTagName('script')[0];
  firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

  var player;
  function onYouTubePlayerAPIReady() {
    player = new YT.Player('player', {
      playerVars: { 'autoplay': 1, 'controls': 1,'autohide':1,'wmode':'opaque' },
      //videoId: 'JW5meKfy3fY',
      events: {
        'onReady': onPlayerReady,
        'onStateChange': onPlayerStateChange,
        'onError': onPlayerError
      }
    });
  }

  function onPlayerError(event) {
  window.location.href = "#";

  }


  function onPlayerReady(event) { 
    //event.target.mute();
  }

  function onPlayerStateChange(event) {
    if(event.data == 0) {
            window.location.href = "#";
            //location.reload();
    }

      

}

+3


source to share


1 answer


You can load the json data of the video before trying to embed and check constraints:

var xmlhttp = new XMLHttpRequest();
var url = 'http://gdata.youtube.com/feeds/api/videos?v=2&alt=jsonc&q=TJC-subagTg';

xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        var response = JSON.parse(xmlhttp.responseText);
        console.log(response.data.items[0].accessControl);
        console.log(response.data.items[0].restrictions);
    }
}
xmlhttp.open("GET", url, true);
xmlhttp.send();

      



http://embed.plnkr.co/P6LqDCdPgTH0HiU3SReR/preview

+1


source







All Articles