0

I am looking to add call to action buttons that only appear during certain time parameters while a video is playing. I am currently using javascript and it is working okay but when I add more than 1 to a video, only one works.

<!-- DO MORE-->
  <pre>
    <div hidden data-starttime=16 data-endtime=35 id="GrowDoMore"><button type="button" class="GrowDoGuide" data-toggle="modal" data-target="#GrowDoModal" id="growDo">Grow: Do More</button></div>
  </pre><!-- Modal -->
    <div class="modal fade" id="GrowDoModal" tabindex="-1" role="dialog" aria-labelledby="Grow: Do More">
      <div class="modal-dialog" role="document">
        <!-- <div class="modal-content">-->
        <a href="http://www.nascireland.org/" target="blank"><img src="imgs/doMore.png" width ="300%" class="center" /></a><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
        <div class="closeButton2"><button type="button" id="growDoClose" class="GrowDoGuide btn growDo" data-dismiss="modal" >Back to Documentary X</button></div>
      </div><!--</modal>-->
    </div><!--</modal>-->
    <!-- END DO MORE-->

var growVideo=document.getElementById("videogrow");
var makeVideo=document.getElementById("videomake");
var videoList=['mikethefrog.mp4','mikethefrog.mp4'];
var index = videoList.indexOf(window.currentVideoName);
var growLearnButton = document.getElementById("growLearn");
var growLearnCloseButton = document.getElementById("growLearnClose");
var growDoButton = document.getElementById("growDo");
var growDoCloseButton = document.getElementById("growDoClose");
var makeLearnButton = document.getElementById("makeLearn");
var makeLearnCloseButton = document.getElementById("makeLearnClose");
var makeDoButton = document.getElementById("makeDo");
var makeDoCloseButton = document.getElementById("makeDoClose");


//Next button
function nextButton(){
growVideo.pause();
growVideo.currentTime=0;
index = index + 1;
if(index==videoList.length)
index = 0;
alert(videoList[index]);
growVideo.src = 'videos/'+videoList[index];
window.currentVideoName=videoList[index];
growVideo.play();
}
function playPause()
{
if (growVideo.paused)
  growVideo.play();
else
  growVideo.pause();
}


//DOC2
//Next button
function nextButton(){
makeVideo.pause();
makeVideo.currentTime=0;
index = index + 1;
if(index==videoList.length)
index = 0;
alert(videoList[index]);
makeVideo.src = 'videos/'+videoList[index];
window.currentVideoName=videoList[index];
makeVideo.play();
}
function playPause2()
{
if (makeVideo.paused)
  makeVideo.play();
else
  makeVideo.pause();
}

//GrowLearnMoreGuide


  // Event listener for the guide buttons
  growLearnButton.addEventListener("click", function() {
    if (growVideo.paused == true) {
      // Play the video
      growVideo.play();

      // Update the button text to 'Pause'
      growLearnButton.innerHTML = 'Grow: Learn More';
    } else {
      // Pause the video
      growVideo.pause();

      // Update the button text to 'Play'
      growLearnButton.innerHTML = 'Grow: Learn More';
    }
  });

  // Event listener for the close buttons
  growLearnCloseButton.addEventListener("click", function() {
    if (growVideo.paused == true) {
      // Play the video
      growVideo.play();

      // Update the button text to 'Pause'
      growLearnCloseButton.innerHTML = 'X Back to Grow';
    } else {
      // Pause the video
      growVideo.pause();

      // Update the button text to 'Play'
      growLearnCloseButton.innerHTML = 'X Back to Grow';
    }
  });

  var GrowLearnMore = document.getElementById('GrowLearnMore');
  var GrowLearnMorestart = GrowLearnMore.getAttribute('data-starttime');
  var GrowLearnMoreend = GrowLearnMore.getAttribute('data-endtime');
  growVideo.ontimeupdate = function(e)
    {
      var hasHidden = GrowLearnMore.hasAttribute('hidden');
      if (growVideo.currentTime > GrowLearnMorestart && growVideo.currentTime < GrowLearnMoreend)
        {
          if (hasHidden) GrowLearnMore.removeAttribute('hidden');
        }
        else
          {
            if (!hasHidden) GrowLearnMore.setAttribute('hidden', '');
          }
        }

        //GrowDoMoreGuide



        // Event listener for the guide buttons
          growDoButton.addEventListener("click", function() {
            if (growVideo.paused == true) {
              // Play the video
              growVideo.play();

              // Update the button text to 'Pause'
              growDoButton.innerHTML = 'Grow: Do More';
            } else {
              // Pause the video
              growVideo.pause();

              // Update the button text to 'Play'
              growDoButton.innerHTML = 'Grow: Do More';
            }
          });

          // Event listener for the close buttons
          growDoCloseButton.addEventListener("click", function() {
            if (growVideo.paused == true) {
              // Play the video
              growVideo.play();

              // Update the button text to 'Pause'
              growDoCloseButton.innerHTML = 'X Back to Part 1: Grow';
            } else {
              // Pause the video
              growVideo.pause();

              // Update the button text to 'Play'
              growDoCloseButton.innerHTML = 'X Back to Part 1: Grow';
            }
          });

          var GrowDoMore = document.getElementById('GrowDoMore');
          var GrowDoMorestart = GrowDoMore.getAttribute('data-starttime');
          var GrowDoMoreend = GrowDoMore.getAttribute('data-endtime');
          growVideo.ontimeupdate = function(e)
            {
              var hasHidden = GrowDoMore.hasAttribute('hidden');
              if (growVideo.currentTime > GrowDoMorestart && growVideo.currentTime < GrowDoMoreend)
                {
                  if (hasHidden) GrowDoMore.removeAttribute('hidden');
                }
                else
                  {
                    if (!hasHidden) GrowDoMore.setAttribute('hidden', '');
                  }
                }
 //Guide2

// Event listener for the guide buttons
  makeLearnButton.addEventListener("click", function() {
    if (makeVideo.paused == true) {
      // Play the video
      makeVideo.play();

      // Update the button text to 'Pause'
      makeLearnButton.innerHTML = 'Grow Food';
    } else {
      // Pause the video
      makeVideo.pause();

      // Update the button text to 'Play'
      makeLearnButton.innerHTML = 'Grow Food';
    }
  });

  // Event listener for the close buttons
  makeLearnCloseButton.addEventListener("click", function() {
    if (makeVideo.paused == true) {
      // Play the video
      makeVideo.play();

      // Update the button text to 'Pause'
      makeLearnCloseButton.innerHTML = 'X Back to Make';
    } else {
      // Pause the video
      makeVideo.pause();

      // Update the button text to 'Play'
      makeLearnCloseButton.innerHTML = 'X Back to Make';
    }
  });

  var MakeLearnMore = document.getElementById('MakeLearnMore1');
  var MakeLearnMorestart = MakeLearnMore1.getAttribute('data-starttime');
  var MakeLearnMoreend = MakeLearnMore1.getAttribute('data-endtime');
  makeVideo.ontimeupdate = function(e)
    {
      var hasHidden = MakeLearnMore.hasAttribute('hidden');
      if (makeVideo.currentTime > MakeLearnMorestart && makeVideo.currentTime < MakeLearnMoreend)
        {
          if (hasHidden) MakeLearnMore.removeAttribute('hidden');
        }
        else
          {
            if (!hasHidden) MakeLearnMore.setAttribute('hidden', '');
          }
        }
.GrowLearnGuide
{
  width: 300px;
  position: relative;
  bottom: 190px;
  float:right;
  background-color: #ffc000;
  border-style: solid;
  z-index: 21474836;
}

.MakeLearnGuide
{
  width: 300px;
  position: relative;
  bottom: 190px;
  float:right;
  background-color: #ffc000;
  border-style: solid;
  z-index: 21474836;
}

  .GrowDoGuide
{
  width: 300px;
  position: relative;
  bottom: 190px;
  float:right;
  background-color: #ffcccc;
  border-style: solid;
  z-index: 21474837;
}

pre
{
  background-color: #2C2C2C;
  border: none;
  z-index: 2147483646;
}

.closeButton1
{
  font-family: 'Quicksand', sans-serif;
  font-size: 26pt;
  color: #000000;
  border: none;
  z-index: 2147483644;
  margin-left:820px;
  margin-top:-528px;
}

.closeButton2
{
  font-family: 'Quicksand', sans-serif;
  font-size: 26pt;
  color: #000000;
  border: none;
  z-index: 2147483644;
  margin-left:820px;
  margin-top:-528px;
}


button {
  background: rgba(0,0,0,.5);
  border: 0;
  color: #fff;
  -webkit-border-radius: 3px;
  -moz-border-radius: 3px;
  -o-border-radius: 3px;
  border-radius: 3px;
  width: 60px;
  height: 60px;
}


button:hover {
  cursor: pointer;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<body>

<!--VIDEO 2 PART 2 -->
<div style="text-align:center">

    <h1>Part One: Grow</h1>

  <button onclick="playPause()">Play/Pause</button>
  <button onclick="nextButton()">Next</button>
  <br>
  <video id="videogrow" width="60%">
    <source src="http://fareireland.ie/mikethefrog.mp4" type="video/mp4">
    <source  type="video/mp3">
     <source type="video/ogg">
    Your browser does not support HTML5 video.
  </video>

  <!-- LEARN MORE-->
  <pre>
    <div hidden data-starttime=1 data-endtime=15 id="GrowLearnMore"><button type="button" class="GrowLearnGuide" data-toggle="modal" data-target="#GrowLearnModal" id="growLearn">Grow: Learn More</button></div>
  </pre><!-- Modal -->
    <div class="modal fade" id="GrowLearnModal" tabindex="-1" role="dialog" aria-labelledby="Grow: Learn More">
      <div class="modal-dialog" role="document">
        <!-- <div class="modal-content">-->
        <a href="http://www.nascireland.org/" target="blank"><img src="imgs/doMore.png" width ="250%" class="center" /></a><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
        <div class="closeButton1"><button type="button" class="GrowLearnGuide btn growLearn" data-dismiss="modal" id="growLearnClose">Back to Documentary X</button></div>
      </div><!--</modal>-->
    </div><!--</modal>-->
  <!-- END LEARN MORE-->
<!-- DO MORE-->
  <pre>
    <div hidden data-starttime=16 data-endtime=35 id="GrowDoMore"><button type="button" class="GrowDoGuide" data-toggle="modal" data-target="#GrowDoModal" id="growDo">Grow: Do More</button></div>
  </pre><!-- Modal -->
    <div class="modal fade" id="GrowDoModal" tabindex="-1" role="dialog" aria-labelledby="Grow: Do More">
      <div class="modal-dialog" role="document">
        <!-- <div class="modal-content">-->
        <a href="http://www.nascireland.org/" target="blank"><img src="imgs/doMore.png" width ="300%" class="center" /></a><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
        <div class="closeButton2"><button type="button" id="growDoClose" class="GrowDoGuide btn growDo" data-dismiss="modal" >Back to Documentary X</button></div>
      </div><!--</modal>-->
    </div><!--</modal>-->
    <!-- END DO MORE-->


  </div>
<!--END OF VIDEO 1 PART 1 -->



<!--VIDEO 2 PART 2 -->
<div style="text-align:center">
  <h1>Part Two: Make</h1>
  <button onclick="playPause2()">Play/Pause</button>
  <button onclick="nextButton2()">Next</button>
<br>
    <video id="videomake" width="60%">
    <source src="http://fareireland.ie/mikethefrog.mp4" type="video/mp4">
    <source  type="video/mp3">
     <source type="video/ogg">
    Your browser does not support HTML5 video.
  </video>

  <pre>
    <div hidden data-starttime=2 data-endtime=25 id="MakeLearnMore1"><button type="button" class="MakeLearnGuide" data-toggle="modal" data-target="#MakeDoModal" id="makeLearn">GROW FOOD</button></div>

  </pre><!-- Modal -->
    <div class="modal fade" id="MakeLearnModal" tabindex="-1" role="dialog" aria-labelledby="Make: Learn More">
      <div class="modal-dialog" role="document">
        <!-- <div class="modal-content">-->
        <a href="http://www.nascireland.org/" target="blank"><img src="imgs/doMore.png" width ="300%" class="center" /></a><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
        <div class="closeButton1"><button type="button" class="MakeLearnGuide btn makeLearn" data-dismiss="modal" id="makeLearnClose">testc Back to Documentary X</button></div>
      </div><!--</modal>-->
    </div><!--</modal>-->

</div>
<!--END OF VIDEO 2 PART 2 -->
<script src="video.js"></script>
<script src="growlearn.js"></script>
<script src="growDo.js"></script>
<script src="makelearn.js"></script>
<script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="bootstrap.min.js"></script>
</body>
</html>

It works to a certain extent but when more than one is added to a video, one will not work.

Here is my code on jsfiddle: https://jsfiddle.net/ktdonohoe/cnma4s6b/

The videos are not working there but essentially the button should appear beside each video during a certain time range like this: enter image description here

I am doing this based on a 2011 article I found: https://www.smashingmagazine.com/2011/03/syncing-content-with-html5-video/

I am wondering if there is a more up to date method for doing this or if anyone knows a solution to this.

user2976972
  • 33
  • 2
  • 6
  • Are you only trying to display a notification at certain times during the media playback, or is element expected to be clicked? Note, `playPause` is not defined at linked jsfiddle. See also [HTML5 audio streaming: precisely measure latency?](https://stackoverflow.com/questions/38768375/html5-audio-streaming-precisely-measure-latency/) – guest271314 Aug 05 '17 at 17:51
  • If you are only trying to display notifications at specific times during media playback and not render an element that is expected to be clicked you can use `` element and `.vtt` file or `VTTCue()`, see [Alternative for checking browser idle](https://stackoverflow.com/questions/39553729/alternative-for-checking-browser-idle/) – guest271314 Aug 05 '17 at 17:57
  • I am trying to generate a button at a specific time, if the button is clicked the video will pause and a bootstrap modal pop-up box will display on top of the video. Once it is closed the video will resume playing from where it left off. – user2976972 Aug 05 '17 at 21:36

0 Answers0