1

I have a HTML5 video that has a poster and a CSS play overlay button. I am trying to get the video to load once it has ended so that it shows the poster and the play overlay button again.

I have tried the following code but get a Parser error on the last line, can anybody help me and let me know what it is I am doing wrong!?

$(document).ready(function() {
  $('.video').parent().click(function () {
    if ($(this).children(".video").get(0).paused) {
      $(this).children(".video").get(0).play();
      $(this).children(".playpause").fadeOut();
    } else {
      $(this).children(".video").get(0).pause();
      $(this).children(".playpause").fadeIn();
    }
  });

  var video= $(".video").get(0);   
  video.addEventListener('ended',function () {
    video.load(); 
    $(".playpause").show();                              
  }, false);
});
GG.
  • 21,083
  • 14
  • 84
  • 130
  • 2
    And the exact error message is...? – j08691 Dec 12 '16 at 20:25
  • 3
    Can't see an error. It would help if you properly indented the code. – putvande Dec 12 '16 at 20:27
  • 1
    Would help if you add the HTML too. – Ionut Necula Dec 12 '16 at 20:28
  • 1
    The code does not cause any errors for me, probably because none of the appropriate conditions or callbacks are executing. In your browser try drilling down/clicking on the error. Use Firebug or Chrome dev tools to see the exact source of the error message. – Alex G Rice Dec 12 '16 at 20:30
  • @j08691 This is the error `undefined:1 Uncaught (in promise) DOMException: The play() request was interrupted by a call to pause().` . To reproduce wait until video has completed, then immediately click `"click"` twice in rapid succession https://jsfiddle.net/q1wnLwa3/1/ – guest271314 Dec 12 '16 at 21:28

1 Answers1

1

To prevent

Uncaught (in promise) DOMException: The play() request was interrupted by a call to pause().

error, wrap if statement within click handler within setTimeout() call.

See also How to prevent "The play() request was interrupted by a call to pause()" error?

$(document).ready(function() {
  $('.video').parent().click(function () {
    var vid = $(this).children(".video").get(0);
    setTimeout(function() {
    if (vid.paused) {
      vid.play();
      $(this).children(".playpause").fadeOut();
    } else {
      vid.pause();
      $(this).children(".playpause").fadeIn();
    }
    })
  });

  var video= $(".video").get(0);   
  video.addEventListener('ended',function () {
    video.load(); 
    $(".playpause").show();                              
  }, false);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
click<br>
<span class="playpause">play pause</span>
<video controls class="video" poster="https://placehold.it/350x150"  src="https://nickdesaulniers.github.io/netfix/demo/frag_bunny.mp4"></video>
</div>
Community
  • 1
  • 1
guest271314
  • 1
  • 15
  • 104
  • 177