Pause all Vimeo videos with API?

2019-05-27 12:33发布

问题:

I have hit a wall and cannot figure out how to make this work. With Vimeo's Advanced API I am pulling in all videos from an account with thumbnails. When clicking on a thumbnail the video is shown above and clicking on a different thumbnail hides the currently shown video. The problem is that the video continues to play even when it is hidden. I have spent a few hours looking at the API and I cannot get it to pause when hidden.

var iframe = $('.video')[0];

  $('.thumbnail a').click(function(e) {

  $f(iframe).api('pause');

  });

The above code pauses only the first video. If I change the number [0] to [1], then the second video pauses when you click on another thumbnail. Does anyone have any thoughts? I am using froogaloop in the above code.

回答1:

I used this with jQuery:

<script type="text/javascript" src="http://a.vimeocdn.com/js/froogaloop2.min.js"></script>
<script type="text/javascript">// <![CDATA[
function pauseAll() {
    $('iframe[src*="vimeo.com"]').each(function () {
        $f(this).api('pause');
    });
}
// ]]></script>

use it on an onclick.

<a href="#" onclick="pauseAll(); return false;">pause all</a>


回答2:

You can use this code to pause all YouTube and Vimeo videos on the page:

$('iframe').each(function() {
  this.contentWindow.postMessage('{"event":"command","func":"pauseVideo","args":""}', '*');
  this.contentWindow.postMessage('{"method":"pause","value":""}', '*');
});