Bootstrap Carousel 中的 Youtube iframe - 在幻灯片上停止视频 [英] Youtube iframes in Bootstrap Carousel - stop video on slide

查看:15
本文介绍了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' 

当我单击轮播控件时.但是,如果我输入

when I click the carousel controls. However, if I type

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):

YouTube iframe API:如何控制 HTML 中已有的 iframe 播放器?

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

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆