Youtube视频暂停时停止事件

Youtube video stop on pause event

本文关键字:事件 视频 暂停 Youtube      更新时间:2023-09-26

我的网站页面上有几个带有电视频道的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>

我正在尝试使用一个java脚本来播放静音视频,并在暂停事件时停止它,但它对我不起作用

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();
}

我需要在暂停事件中完全停止视频,因为这是电视直播频道,所以如果我现在按下暂停,10分钟后我会得到一个记录,但不是直播。我怎样才能让我的剧本发挥作用?

更新

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)

工作演示

柱塞


停止YouTube播放器

  1. 使用contentWindow方法访问iframe
  2. 通过iframe与postMessage API进行下一次跨域通信是可能的
  3. YouTube iFrame API的下一篇文章命令

相关规范

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

工作演示

FIDDLE

代码段

<!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>