Eddy
Eddy

Reputation: 566

How can I unload video when it has finished playing?

I am trying to unload a video when it has finished playing. No luck so far. Can anyone help please? JSFIDDLE example here

$(document).ready(function(){
    $('#video').on('ended',function(){
      $('#video').get(0).unload();
    });
});

Upvotes: 2

Views: 702

Answers (1)

Transattic
Transattic

Reputation: 40

I have included the solution at jsfiddle and in this snippet below. Note that I used .toggle() instead of .unload(), since .unload() is meant to be fired on window event :

https://jsfiddle.net/firelite/h4aqkgo1/

$('#videoButton').on('click', function() {
  var videoDiv = $('#videoDiv').toggle();

  if (videoDiv.is(':visible')) {
    $('#video').get(0).load();
    $('#video').get(0).play();
  } else {
    $('#video').get(0).pause();
  }
});

$(document).ready(function() {
  $('#video').on('ended', function() {
    $('#video').get(0).pause();
    $('#videoDiv').toggle();
  });
});
#videoDiv {
  width: 100%;
  height: 360px;
  position: relative;
}
#videoBlock,
#videoMessage {
  width: 100%;
  height: 360px;
  position: absolute;
  top: 0;
  left: 0;
}
.videoClick {
  text-align: center
}
.videoClick a {
  color: white;
  background-color: rgba(241, 241, 241, 0.25);
  font-size: 1.7em;
  cursor: pointer;
  cursor: hand
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="videoButton">Toggle video</button>
<div id="videoDiv" style="display:none">
  <div id="videoBlock">
    <video preload="preload" id="video">
      <source src="https://static.webshopapp.com/shops/054833/files/093143183/fotel-photography-course-tour-workshop-video-4.mp4" type="video/mp4">
    </video>
  </div>
</div>

Upvotes: 1

Related Questions