Already embedded iframe not working with the youtu

2019-09-19 03:35发布

问题:

My iframe html

<iframe id="player" frameborder="0" width="660" height="371" allowfullscreen="" src="https://www.youtube.com/embed/CMm6tDavSXg?feature=oembed">

My youtube js

  var tag = document.createElement('script');

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

var player;
function onYouTubeIframeAPIReady() {
  player = new YT.Player('player', {
    events: {
 'onReady': onPlayerReady,
      'onStateChange': onPlayerStateChange
    }
  });
}
function onPlayerReady(event) {
        event.target.playVideo();
      }

But still it does not start playing or anything else... Where is the issue here? Thank you

回答1:

  • onPlayerReady will not fire the ready check on localhost.
  • Also when linking your youtube.js file it has to come after the iframe.
  • add ?enablejsapi=1
  • sometimes double linking in both player_api and iframe_api will also help
  • //< before www. not https://
  • placment is key.

EDIT: the problem is with the fact that you also need to add ?enablejsapi=1 to the video embed link.

//www.youtube.com/embed/F4efZDqXZKA?enablejsapi=1

fiddle: http://jsfiddle.net/y89je0k8/

That will solve your problem.