Youtube视频停止暂停活动

时间:2016-07-19 14:54:48

标签: javascript video youtube

我的网站页面上有几个带有电视频道的youtube iframe。



<div>
<iframe id="ytplayer"  src="https://www.youtube.com/embed/ntBxGznOML0?rel=0&enablejsapi=1&autoplay=0&controls=1&showinfo=0&loop=1&iv_load_policy=3&modestbranding=0" frameborder="0" allowfullscreen></iframe>
</div>
<div>
<iframe id="ytplayer"  src="https://www.youtube.com/embed/zl4aeAfhdro?rel=0&enablejsapi=1&autoplay=0&controls=1&showinfo=0&loop=1&iv_load_policy=3&modestbranding=0" frameborder="0" allowfullscreen></iframe>
</div>
<div>
<iframe id="ytplayer"  src="https://www.youtube.com/embed/URD4UWm-edg?rel=0&enablejsapi=1&autoplay=0&controls=1&showinfo=0&loop=1&iv_load_policy=3&modestbranding=0" frameborder="0" allowfullscreen></iframe>
</div>
<div>
<iframe id="ytplayer"  src="https://www.youtube.com/embed/75j9l956bVs?rel=0&enablejsapi=1&autoplay=0&controls=1&showinfo=0&loop=1&iv_load_policy=3&modestbranding=0" frameborder="0" allowfullscreen></iframe>
</div>
&#13;
&#13;
&#13;

我试图使用一个java脚本来播放静音视频并在暂停事件中停止它,但它对我不起作用。该脚本正在关注

&#13;
&#13;
var player;

function onYouTubeIframeAPIReady() {
    player = new YT.Player('ytplayer', {
        events: {
            'onReady': onPlayerReady,
            'onStateChange': onPlayerStateChange
            
        }
    });
}
function onPlayerStateChange(event){
     player.stopVideo(0);
         
  }
   

}
function onPlayerReady(event) {
    player.mute();
    player.playVideo();
}
&#13;
&#13;
&#13;

我需要在暂停事件中完全停止视频,因为它是直播电视频道,所以如果我现在按暂停,在10分钟内我会得到一个记录但不是直播。如何让我的脚本工作?

1 个答案:

答案 0 :(得分:2)

更新

OP有多个播放器并使用纯JavaScript,以下更改为:

  1. 多个玩家共享相同的ID,现在每个玩家都有一个唯一的ID。
  2. 为每位玩家添加了<button class='stop' data-id='yt*'>STOP</button>
    • 每个按钮都有一个data-id。它的价值对应于它的玩家ID。
  3. 围绕一切包裹<section id='videoChannels'></section>
    • eventListener添加到#videoChannels,以便点击按钮时#videoChannels被称为target.currentTarget,可与event.target进行比较以确定单击的按钮(即event.target)。
    • 事件链capture eventPhase到达event.target(点击的按钮)后,在与该按钮对应的播放器上调用函数ytCommand()(参见步骤2)
  4. 工作演示

    PLUNKER

    停止播放YouTube播放器

    1. 使用contentWindow方法
    2. 访问iframe
    3. 使用postMessage API可以通过iframe进行下一次跨域通信。
    4. 来自YouTube iFrame API
    5. 的下一个帖子命令

      相关代码

      $('#stop').on('click', function() {
      
        $('#ytPlayer')[0].contentWindow.postMessage('{"event":"command","func":"' + 'stopVideo' + '","args":""}', '*');
      });
      

      工作演示

      FIDDLE

      &#13;
      &#13;
      <!doctype html>
      <html>
      
      <head>
        <meta charset="utf-8">
        <title>Stop YTPlayer</title>
      </head>
      
      <body>
        <section id="videoChannels">
          <div>
            <iframe id="yt0" class="player" src="https://www.youtube.com/embed/8IzxdjVr5ZI?enablejsapi=1" frameborder="0" allowfullscreen></iframe>
            <button class='stop' data-id='yt0'>STOP</button>
          </div>
      
          <div>
            <iframe id="yt1" class="player" src="https://www.youtube.com/embed/AhenpCh6BO4?enablejsapi=1" frameborder="0" allowfullscreen></iframe>
            <button class='stop' data-id='yt1'>STOP</button>
          </div>
      
          <div>
            <iframe id="yt2" class="player" src="https://www.youtube.com/embed/HrmF-mPLybw?enablejsapi=1" frameborder="0" allowfullscreen></iframe>
            <button class='stop' data-id='yt2'>STOP</button>
          </div>
      
          <div>
            <iframe id="yt3" class="player" src="https://www.youtube.com/embed/6KouSwLP_2o?enablejsapi=1" frameborder="0" allowfullscreen></iframe>
            <button class='stop' data-id='yt3'>STOP</button>
          </div>
        </section>
        <script>
          var vidCh = document.getElementById('videoChannels');
      
          vidCh.addEventListener('click', ytCommand, false);
      
          function ytCommand(event) {
            if (event.target != event.currentTarget) {
              var btn = event.target.getAttribute('data-id');
              var ytp = document.getElementById(btn);
              ytp.contentWindow.postMessage('{"event":"command","func":"' + 'stopVideo' + '","args":""}', '*');
            }
          }
        </script>
      </body>
      
      </html>
      &#13;
      &#13;
      &#13;