如何根据要单击的按钮动态播放视频

时间:2019-04-25 09:57:52

标签: javascript html html5 video html5-video

您好,我有2个按钮的预告片和电影,我想根据单击的按钮来播放视频。

例如,我选择“预告片”,那么它应该播放预告片,反之亦然

这是我的按钮

 <button class="btn btn-sm btn-dark" id="trailer" data-toggle="modal" data-target="#myModal2">Trailer</button>

 <button class="btn btn-sm btn-dark" id="movieNoads" data-toggle="modal" data-target="#myModal2">Movie</button>

这是我的视频播放器及其javascript代码

 <video controls playsinline id="player" width="100%">

      <script type="text/javascript">

        var video = document.getElementById('video');
        var source = document.createElement('source');

         document.getElementById('trailer').onclick = function () {

            source.setAttribute('src', '../inflightapp/storage/app/public/trailer_videos/<?php echo ''.$row2['trailer_video'].''; ?>');
            video.appendChild(source);
            video.play();

          }


          document.getElementById('movieNoads').onclick = function () {
              source.setAttribute('src', '../inflightapp/storage/app/public/movie_videos/<?php echo ''.$row2['movie_video'].''; ?>');
            video.appendChild(source);
            video.play();
          }

      </script>

    </video>

4 个答案:

答案 0 :(得分:0)

您可以看一下。

<button class="btn btn-sm btn-dark" id="trailer">Trailer</button>

<button class="btn btn-sm btn-dark" id="movieNoads">Movie</button>

<script type="text/javascript"> 

    var trailer = document.getElementById('trailer');
    trailer.setAttribute('data-toggle', 'modal'); 
    trailer.setAttribute('data-target', '#myModal2');

   var movieNoads = document.getElementById('movieNoads');
   movieNoads.setAttribute('data-toggle', 'modal'); 
   movieNoads.setAttribute('data-target', '#myModal2');
</script>
 
 <video controls playsinline id="player" width="100%">

      <script type="text/javascript">

        var video = document.getElementById('player');

         document.getElementById('trailer').onclick = function () {
            document.getElementById("player").style.display = "block";
            video.setAttribute('src', 'YouVideoURLHere');
            video.play();

          }


          document.getElementById('movieNoads').onclick = function () {               document.getElementById("player").style.display = "block";
            video.setAttribute('src', 'YourVideoURLHere');
            video.play();
          }
      document.getElementById("player").style.display = "none";

      </script>

    </video>

答案 1 :(得分:0)

您忘了load视频

"use strict";

console.clear();

const video = document.getElementById("player");
const source = document.createElement("source");

function loadVideo(element, src) {
  source.src = src;
  element.appendChild(source);
  element.load();
  element.play();
}

document.getElementById("trailer").addEventListener(
  'click',
  () => loadVideo(
    video,
    "https://s3-us-west-2.amazonaws.com/s.cdpn.io/96344/lego.mp4"
  )
)

document.getElementById("movieNoads").addEventListener(
  'click',
  () => loadVideo(
    video,
    "https://s3-us-west-2.amazonaws.com/s.cdpn.io/96344/abstract-001.mp4"
  )
)
<button class="btn btn-sm btn-dark" id="trailer" data-toggle="modal" data-target="#myModal2">Trailer</button>

<button class="btn btn-sm btn-dark" id="movieNoads" data-toggle="modal" data-target="#myModal2">Movie</button>

<video controls playsinline id="player" width="100%" loop></video>

答案 2 :(得分:0)

您可以尝试

function setvideo(src) {
    document.getElementById('div_video').innerHTML = '<video autoplay controls id="video_ctrl" style=" width: 100%;"><source src="'+src+'" type="video/mp4"></video>';
    document.getElementById('video_ctrl').play();
}
<!DOCTYPE html>
<html>
<body>

<div id="div_video"> </div>
<button onClick="setvideo('movie.mp4')"> Trailer</button>
<button onClick="setvideo('mov_bbb.mp4')"> Movie</button>




</body>
</html>

答案 3 :(得分:0)

欢迎使用堆栈溢出。您可以尝试以下方法:

我使用jQuery和Bootstrap(因为我注意到您使用了Bootstrap HTML元素)。

//Custom function to populate a modal. This could be expanded on to even add a destination parameter in order to use seperate modals (e.g. function videoModal(destination, source) and replace #myModal2 with the `destination` parameter).
function videoModal(source) {
  $("#myModal2 .modal-body").html($("video#player").clone());
  //Add your second source:
  $("#myModal2 video#player > source").attr("src", source);
  $("#myModal2 .modal-body video#player").css("display", "block");
}

$(document).ready(function() {
  //On init, save current loaded state of modal
  let modalInit = $("#myModal2 .modal-body").html();

  //On interaction with a control
  $("body").on("click", "button#trailer", function() {
    //Populate modal
    videoModal("https://www.w3schools.com/tags/movie.mp4");
  });

  $("body").on("click", "button#movieNoads", function() {
    //Populate modal
    videoModal("https://www.w3schools.com/html/mov_bbb.mp4")
  });

  //Reset modal on close
  $("#myModal2").on('hidden.bs.modal', function(e) {
    $("#myModal2 .modal-body").html(modalInit);
  });
});
video#player {
  display: none;
  margin-left: auto;
  margin-right: auto;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<!-- Modal -->
<div class="modal fade" id="myModal2" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>

<video id="player" controls autoplay loop>
  <source src="" type="video/mp4">
</video>

<!-- Button trigger modal -->
<button type="button" id="trailer" class="btn btn-primary" data-toggle="modal" data-target="#myModal2">
  Trailer
</button>

<button type="button" id="movieNoads" class="btn btn-primary" data-toggle="modal" data-target="#myModal2">
  Movie
</button>

JSFiddle

这样,您还可以将视频加载到模式模块中(正如我注意到的那样,您尝试使用模式按钮)。无需刷新。