mardi 14 juin 2016

YouTube video is not showing

For many hours now, I try to do simple actions with YouTube API. Except the fact that I don't get many things in the API, I just wanted to understand one simple thing: how come my video isn't showing in my jsfiddle, even though, to be sure, I copied the entire code from another jsfiddle and didn't change a thing.

Here is the jsfiddle: http://jsfiddle.net/jpreynat/e11oy0eu/

My final goal was to use a simple JQuery to add and remove a class to the body while clicking play or pause. Here is my final code: https://jsfiddle.net/us64a5os

// 2. This code loads the IFrame Player API code asynchronously.
  var tag = document.createElement('script');

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

  // 3. This function creates an <iframe> (and YouTube player)
  //    after the API code downloads.
  var player;
  function onYouTubeIframeAPIReady() {
    player = new YT.Player('player', {
      height: '390',
      width: '640',
      videoId: 'M7lc1UVf-VE',
      events: {
        'onReady': onPlayerReady,
        'onStateChange': onPlayerStateChange
      }
    });
  }

  // 4. The API will call this function when the video player is ready.
  function onPlayerReady(event) {
    event.target.playVideo();
  }

  function onPlayerStateChange(event) {
    switch(event.data) {
      case 1:
        $('body').addClass('play');
        break;
      case 2:
        $('body').removeClass('play');
    }
  }

Looking at the different errors in the console, I tried to set https to http or simply to //, I removed Adblock, I tried on a local file on my desktop, etc...

Thanks =)

Aucun commentaire:

Enregistrer un commentaire