使用youtube API中的按钮播放和暂停iframe视频

时间:2015-12-29 22:03:59

标签: javascript iframe youtube youtube-api pause

我有一个iframe youtube视频我想播放并使用按钮暂停,这是我到目前为止但它似乎没有工作,任何人都可以让我知道我做了什么错误。< / p>

<body>



<div class="youtubePlayer" id="youtubePlayer">
<iframe id="videoPlayer" width="340" height="200"      src="https://www.youtube.com/embed/-0oZNWif_jk?list=PLD0qnaHPys3v_j-yGcnA3JJoTCFz_aK7s?enablejsapi=1&html5=1" frameborder="0" allowfullscreen>  </iframe>
</div>


<button id="play-btn">Play</button>
<button id="stop-btn">Stop</button>


<script>



var videoPlayer;

function onYouTubePlayerAPIReady() {
  videoPlayer = new YT.Player('videoPlayer', {
      events: {
          //calls function when player is ready
        'onReady': onPlayerReady
        }
      });
}

function onPlayerReady(event) {
    //bind
    var playButton = document.getElementById("play-btn");
    playButton.addEventListener("click", function() {
        videoPlayer.playVideo();    
    });

    var pauseButton = document.getElementById("stop-btn");
    pauseButton.addEventListener("click", function() {
        player.pauseVideo();
    });
}


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


</script>

</body>

1 个答案:

答案 0 :(得分:1)

应为videoPlayer.pauseVideo();