Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript/youtube api check state for playback restrictions


javascript/youtube api check state for playback restrictions


The problem is that i embed from gdata/youtube-api and sometimes it returns playback video restrictions or embed not allowed. So i was checking for a state or something to handle this. i tried adding the &format=5. & tried the -1 event. With no luck. Any ideas would be greatly appreciated. function onPlayerStateChange(event) { if(event.data == -1){//rest of code}} Thanks in advance.

Edit3/Solution to this problem: as you have the onError set to a function in that function capture event and check for error was a solution to this problem. function onPlayerError(event) {if(event.data === 150 || event.data === 101) {//rest here}}

EDIT: adding more of the code. EDIT 2: errorcode matching is the answer the only problem now is that i don't know how to capture just the errorcode 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();
    }

}

like image 559
EVX Avatar asked Sep 01 '26 09:09

EVX


1 Answers

You could load the json data about the video before trying to embed and check the restrictions:

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

like image 157
jimbo.io Avatar answered Sep 02 '26 22:09

jimbo.io



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!