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

Bootstrap Carousel - Autoplay video on active slide

我一直在尝试使用 Bootstrap Carousel 制作交互式纪录片。为此,我希望活动幻灯片上的视频自动播放。我读过除非视频静音,否则无法自动播放,但我不介意您只需单击一次音量按钮即可自动播放所有内容,但我还不知道如何执行此操作。在 Chrome 上,没有任何内容会自动播放,您必须手动播放每个视频。使用 Safari 时,一旦用户之前与该页面进行过交互,视频就会自动播放,但问题是它们会同时自动播放,即使幻灯片上的视频未处于活动状态(有声)。有人知道解决方案吗?

我的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>

我的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');

});

提前致谢。

您可以这样使用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>