简体   繁体   English

Bootstrap Carousel - 在活动幻灯片上自动播放视频

[英]Bootstrap Carousel - Autoplay video on active slide

I've been trying to make an interactive documentary using Bootstrap Carousel.我一直在尝试使用 Bootstrap Carousel 制作交互式纪录片。 For this, I want the video on the active slide to autoplay.为此,我希望活动幻灯片上的视频自动播放。 I've read that autoplay isn't possible unless the video is muted, but I don't mind it if you only had to click a volume button once for everything to then autoplay but I haven't figured out how to do this.我读过除非视频静音,否则自动播放是不可能的,但我不介意如果你只需要点击一次音量按钮就可以自动播放,但我还没有想出如何做到这一点。 On Chrome, nothing autoplays and you have to manually play every video.在 Chrome 上,没有什么会自动播放,您必须手动播放每个视频。 When using Safari, once the user has interacted with the page before, the video's do autoplay, but the problem is that they all autoplay at the same time, even the video's on the slides that aren't active (with sound).使用 Safari 时,一旦用户之前与页面进行过交互,视频就会自动播放,但问题是它们都同时自动播放,即使幻灯片上的视频未处于活动状态(带声音)。 Does anyone know a solution?有谁知道解决方案?

My HTML:我的 HTML:

<div id="myCarousel" class="carousel slide" data-interval="false">
<ol class="carousel-indicators">
    <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
    <li data-target="#myCarousel" data-slide-to="1"></li>
    <li data-target="#myCarousel" data-slide-to="2"></li>
  </ol>

  <div class="carousel-inner">
    <div class="carousel-item active"> 
        <video controls autoplay loop muted class="myvid" id="player">
            <source src="assets/img/intro.mp4" type="video/mp4">
            </video>
    </div>

    <div class="carousel-item">
         <video controls autoplay class="myvid" id="player2">
            <source src="assets/img/Placeholder Video.mp4" type="video/mp4">
            </video>
 </div>

  </div>

  <a class="carousel-control-prev" href="#myCarousel" role="button" data-slide="prev">
    <span class="carousel-control-prev-icon" aria-hidden="true"></span>
    <span class="sr-only">Vorige</span>
  </a>
  <a class="carousel-control-next" href="#myCarousel" role="button" data-slide="next">
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
    <span class="sr-only">Volgende</span>
  </a>
</div>

My Javascript:我的Javascript:

  
}).on('slide.bs.carousel', function () {
  document.getElementById('player').pause();
});

/* SLIDE ON CLICK */ 

$('.carousel-indicators > li > a').click(function() {

    // grab href, remove pound sign, convert to number
    var item = Number($(this).attr('href').substring(1));

    // slide to number -1 (account for zero indexing)
    $('#myCarousel').carousel(item - 1);

    // remove current active class
    $('.carousel-indicators .active').removeClass('active');

    // add active class to just clicked on item
    $(this).parent().addClass('active');

    // don't follow the link
    return false;
});

/* AUTOPLAY NAV HIGHLIGHT */

// bind 'slid' function
$('#myCarousel').bind('slid', function() {

    // remove active class
    $('.carousel-indicators .active').removeClass('active');

    // get index of currently active item
    var idx = $('#myCarousel .item.active').index();

    // select currently active item and add active class
    $('.carousel-indicators li:eq(' + idx + ')').addClass('active');

});

Thanks in advance.提前致谢。

You can use Bootstrap 4 Carousel Events this way:您可以通过以下方式使用Bootstrap 4 Carousel Events

 let allVids = $("#myCarousel").find('.carousel-item'); allVids.each(function(index, el) { if (index !== 0) { $(this).find('video')[0].pause(); } }); $("#myCarousel").on('slide.bs.carousel', function(ev) { let slides = $(this).find('.carousel-item'); let pvid = slides[ev.from].querySelectorAll('video')[0]; let vid = slides[ev.to].querySelectorAll('video')[0]; let isPlaying = vid.currentTime > 0 && vid.readyState > 2; vid.play(); if (isPlaying) { pvid.pause(); } });
 #myCarousel { max-width: 1200px; margin: 0 auto; } .carousel-control-next, .carousel-control-prev { width: 9% !important; } .carousel-inner { background: #111; } .carousel-indicators { bottom: -13px !important; } .carousel-item { padding-bottom: 25px; } video { width: 100%; }
 <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script> <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.0/js/bootstrap.min.js" integrity="sha384-3qaqj0lc6sV/qpzrc1N5DC6i1VRn/HyX4qdPaiEFbn54VjQBEU341pvjz7Dv3n6P" crossorigin="anonymous"></script> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.0/css/bootstrap.min.css" integrity="sha384-SI27wrMjH3ZZ89r4o+fGIJtnzkAnFs3E4qz9DIYioCQ5l9Rd/7UAa8DHcaL8jkWt" crossorigin="anonymous"> <div id="myCarousel" class="carousel slide" data-interval="false"> <ol class="carousel-indicators"> <li data-target="#myCarousel" data-slide-to="0" class="active"></li> <li data-target="#myCarousel" data-slide-to="1"></li> <li data-target="#myCarousel" data-slide-to="2"></li> </ol> <div class="carousel-inner"> <div class="carousel-item active"> <video controls autoplay loop muted class="myvid" id="player"> <source src="http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4" type="video/mp4"> </video> </div> <div class="carousel-item"> <video controls autoplay class="myvid" id="player2"> <source src="http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4" type="video/mp4"> </video> </div> <div class="carousel-item"> <video controls autoplay class="myvid" id="player2"> <source src="http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerBlazes.mp4" type="video/mp4"> </video> </div> </div> <a class="carousel-control-prev" href="#myCarousel" role="button" data-slide="prev"> <span class="carousel-control-prev-icon" aria-hidden="true"></span> <span class="sr-only">Vorige</span> </a> <a class="carousel-control-next" href="#myCarousel" role="button" data-slide="next"> <span class="carousel-control-next-icon" aria-hidden="true"></span> <span class="sr-only">Volgende</span> </a> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM