强制嵌入youtube视频静音和重播

时间:2015-02-04 08:06:27

标签: javascript iframe youtube

当我嵌入视频时,我没有成功访问youtube iframe api。

这是我的代码:

 <iframe id="player" width="560" height="315" src="//www.youtube.com/embed/xWPDujqIwZc?autoplay=1" frameborder="0" enablejsapi="1"></iframe>
    <script type="text/javascript" src="youtube.js"></script>

这是#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();
    player.mute();
}

function onPlayerStateChange(event) {
if (event.data == 0) 
    {
        event.target.playVideo();
    }
}

感谢您的帮助!

0 个答案:

没有答案