本文介绍了Bootstrap Carousel中的YouTube iframe-在幻灯片上停止视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在页面上有一堆轮播,每个轮播都混合了视频和图像。我想在用户通过单击轮播控件之一(向左或向右)退出视频时停止播放Youtube视频。

I have a bunch of Carousels on a page, each with a mix of videos and images. I'd like to stop Youtube videos from playing when the user exits a video by clicking one of the carousel controls (left or right).

我希望能够检测到用户何时单击控件,根据当前正在播放的视频创建播放器,然后关闭该视频。在我看到的其他示例中,播放器是在onYoutubeIframeAPIReady函数中声明的,但是我认为最好不要为页面上的每个视频动态创建播放器。现在,我遇到了错误

I'd like to be able to detect when the user clicks the controls, create a player based on the current video that was playing, and turn that video off. In the other examples I've seen, the players were declared in the onYoutubeIframeAPIReady function, but I thought it would be better not to dynamically create players for every video on the page. Right now, I'm getting the error

Uncaught TypeError: Object #<T> has no method 'stopVideo'

但是,如果我键入

player.stopVideo();

在Chrome JavaScript控制台中,可以正常工作。我在做什么错了?

in the Chrome javascript console, it works fine. What am I doing wrong?

  <script>
  var youtubeReady = false;

  function onYouTubeIframeAPIReady(){
    youtubeReady = true;
  }

  $('.carousel').on('slide', function(){
    if(youtubeReady){
      console.log("setting player");
      var iframeID = $(this).find('.active').find('iframe').attr("id");
      player = new YT.Player(iframeID);
      player.stopVideo();
    }
  });
  </script>

轮播示例:

<div class="mainPhoto carousel slide 523" id="carousel-523">
          <div class="carousel-inner 523">
          <div class="item active">
             <div class="flex-video">
                 <a class="fancybox" href="http://www.youtube.com/embed/4pEqbs0ISaw?version=3&amp;enablejsapi=1" rel="gallery 523" data-fancybox-type="iframe">
                     <iframe src="http://www.youtube.com/embed/4pEqbs0ISaw?version=3&amp;enablejsapi=1" id="1188">
                     </iframe>
                 </a>
             </div>
         </div>
        <div class="item">
             <a class="fancybox" href="https://buildinprogress.s3.amazonaws.com/image/image_path/1189/2013-07-05_19.47.55.jpg" rel="gallery 523" data-fancybox-type="image">
                 <img alt="Preview_2013-07-05_19.47.55" id="1189" src="https://buildinprogress.s3.amazonaws.com/image/image_path/1189/preview_2013-07-05_19.47.55.jpg" width="100%">
            </a>
        </div>
    </div>
            <a class="carousel-control left" href="#carousel-523" data-slide="prev" style="display: none;">‹</a>
            <a class="carousel-control right" href="#carousel-523" data-slide="next" style="display: none;">›</a>
        </div>


推荐答案

我最终在这里使用了callPlayer函数(完全不需要使用youtube API):

I ended up using the callPlayer function here (which didn't require using the youtube API at all):

这篇关于Bootstrap Carousel中的YouTube iframe-在幻灯片上停止视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 02:11