Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

youtube iframe api loadVideoById() error

I am adding youtube video to a companies website and would like them to display on non-flash devices. I have been playing with the youtube iframe API and updated one of their examples to allow a user to click on a link to change the video in the iframe. The edited code is:

<!DOCTYPE HTML>
<html>
<body>
<div id="player"></div>
<script>
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 done = false;
var player;
function onYouTubePlayerAPIReady() {
    player = new YT.Player('player', {
      height: '390',
      width: '640',
      videoId: 'JW5meKfy3fY',
      events: {
        'onReady': onPlayerReady,
        'onStateChange': onPlayerStateChange
      }
    });
}
function onPlayerReady(evt) {
    evt.target.playVideo();
}
function onPlayerStateChange(evt) {
    if (evt.data == YT.PlayerState.PLAYING && !done) {
        setTimeout(stopVideo, 6000);
        done = true;
    }
}
function stopVideo() {
    player.stopVideo();
}

function loadVideo(videoID) {
  if(player) { player.loadVideoById(videoID); }
}

</script>

<a href="javascript:loadVideo('kGIjetX6TBk');">Click me to change video</a>

</body>
</html>

The only thing I added was:

function loadVideo(videoID) {
  if(player) { player.loadVideoById(videoID); }
}

This works fine in Safari, Chrome and Firefox but does not work in IE7, 8 or 9. In IE7 and 8 it returns an error "Object does not support this property or method".

Is this an issue with the API or am I doing something wrong?

like image 746
pheedsta Avatar asked Mar 20 '12 06:03

pheedsta


People also ask

What is iFrame API?

Developers can use the iFrame API to programmatically create and interact with an Embed or with multiple Embeds in the same web app. The iFrame API includes methods that you can use to start playback, change the content rendering in an Embed, or stop playback.

How do I unMute an iFrame video?

Until and unless you have allow="autoplay;" in your iframe tag you wont be able to unmute the video. Add this attribute and further unMute function will work. Save this answer.

What is YT player?

Youtube Player. YTPlayer helps you control all of your opened Youtube videos either using keyboard shortcuts or directly using its popup window. The player has audio controls + focus button which focus on a tab, skip button which skip the ad if any, and a button to show suggested youtube videos (exclamation mark icon).


2 Answers

I had a similar problem, and it turned out that you shouldn't call any of the methods on the YT.Player object (including loadVideoById) as long as onPlayerReady hasn't been called.

Doing a check if(player) {...} isn't sufficient, the Player object will be created and some properties will already be available in out without the methods you need being available.

like image 110
pvolders Avatar answered Nov 03 '22 08:11

pvolders


You will need to call the load video function from the onPlayerReady event.

For example, if you want to load a video when clicking a thumbnail do this (this would require jquery but it should get the point across):

function onPlayerReady(evt) {

  evt.target.playVideo();

  //declare the click even function you want
  $('#thumbs a).click(function(){

    //get a data-video-id attr from the <a data-video-id="XXXXXX">
    var myvideo = $(this).attr('data-video-id');

    //call your custom function
    loadVideo(myvideo);

    //prevent click propagation 
    return false;
  });
}

This way you can be sure the player is loaded.

like image 34
Ben Routson Avatar answered Nov 03 '22 08:11

Ben Routson