18

I have a play image with text 'Watch video', when clicked would open up a bootstrap modal window to play the video i put in. I've got the moday view to work. The problem I'm having with this is when I play the video and exit the screen while it is being played, the video continues to play in the background. If I click on the Watch Video link again, it would play the video from where it stopped off previously.

I need to have the video stop when the modal window is closed. It needs to then play the video from the start the next time I click the "Watch video" link. I'm still new to this and am not sure how to do this. Anyone know how I can achieve this?

<div class="col-md-12 f-play" data-toggle="modal" data-target="#myModal">
    <img class="f-play-image" src="images/play.svg" data-target="#myModal" data-video-fullscreen="">Watch video
</div>

<div id="myModal" class="modal fade" role="dialog" tabindex='-1'>
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-body">
                <div class="embed-responsive embed-responsive-16by9">
                    <iframe class="embed-responsive-item" src="https://www.youtube.com/embed/7pvci1hwAx8?" allowfullscreen="" width="640" height="360" frameborder="0"></iframe>
                 </div>
             </div>
             <div class="modal-footer">
                 <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
             </div>

        </div>
    </div>
</div>
Michelle Ashwini
  • 910
  • 2
  • 13
  • 28
  • Does this answer your question? [Twitter Bootstrap Modal stop Youtube video](https://stackoverflow.com/questions/13799377/twitter-bootstrap-modal-stop-youtube-video) – gre_gor Feb 14 '23 at 19:54

7 Answers7

34

I managed to get it to work with the link that @Leo provided. But it was slow as it reloaded the frame everytime the link was clicked. Tried @guillesalazar solution from Twitter Bootstrap Modal stop Youtube video and it worked perfectly. This was the solution I used.

$("#myModal").on('hidden.bs.modal', function (e) {
    $("#myModal iframe").attr("src", $("#myModal iframe").attr("src"));
});
Community
  • 1
  • 1
Michelle Ashwini
  • 910
  • 2
  • 13
  • 28
4

Well you can simply remove the src of the iframe and the put back again like ;

 <iframe id="videosContainers" class="yvideo" src="https://www.youtube.com/embed/kjsdaf  ?>" w></iframe>
 <span onclick="stopVideo(this.getAttribute('vdoId'))" vdoId ="yvideo" id="CloseModalButton"  data-dismiss="modal"></span>

now the Jquery part

function stopVideo(id){
  var src = $j('iframe.'+id).attr('src');
  $j('iframe.'+id).attr('src','');
  $j('iframe.'+id).attr('src',src);

}
Kazi Hasan Ali
  • 311
  • 4
  • 9
2

You just need to stop the video on the hidden.bs.modal event:

$('#myModal').on('hidden.bs.modal', function () {
    player.stopVideo();
   // or jQuery
   $('#playerID').get(0).stopVideo();
   // or remove video url
   $('playerID').attr('src', '');
})

Here is a very similar (if not equal) to what you need to do: http://www.tutorialrepublic.com/codelab.php?topic=faq&file=play-youtube-video-in-bootstrap-modal

They do not use the iframe API so everytime they close the modal they just delete the video url .attr('src', '');

Leonardo Lanchas
  • 1,616
  • 1
  • 15
  • 37
  • 1
    I've added this script, but it still does not stops the video when I click close. – Michelle Ashwini Oct 24 '16 at 07:00
  • What version of Bootstrap are you using? Are you using the Youtube iframe API? Can you post a working example? – Leonardo Lanchas Oct 24 '16 at 07:06
  • I'm using version 3.3.7 of Bootstrap. I dont think I'm using the youtube API. I'll add my code to codepen and link it here – Michelle Ashwini Oct 24 '16 at 07:11
  • I have edited the answer, with another example where the video is embedded manually in an iframe – Leonardo Lanchas Oct 24 '16 at 10:33
  • Thank you! I put my existing code into codepen and it wasnt working. I did find a [solution](http://codepen.io/filippoq/pen/QwogWz/) , but could not understand it very much. The link you included helped me solve my problem and I understand it. Thanks!:) – Michelle Ashwini Oct 25 '16 at 02:34
2

For multiple modals with iframe videos, here is the solution:

$('#myModal').on('hidden.bs.modal', function(e) {
  var $iframes = $(e.target).find('iframe');
  $iframes.each(function(index, iframe){
  $(iframe).attr('src', $(iframe).attr('src'));
  });
})

On modal close, it goes through all the iframe videos and resets them instead of resetting all of them to the first video.

  • You say "multiple modals" but then specify '#myModal' in the code, which is a unique identifier for a single modal ID, therefore by definition not multiple. How would you actually write a jQuery function to deal with a page that generates multiple modals? (that is, how to dynamically select the modal that is actually opened, among the multiples?) – m.arthur Aug 25 '21 at 20:55
1

// Simple and clean

Try using class or ID before Iframe to refresh/reload the particular video. Otherwise, it will reload all iframe source.

$( id/class + 'iframe').attr('src', $('iframe').attr('src'));

sam ruben
  • 365
  • 2
  • 6
1
**If you want to open modal with iframe and remove on close best and short answer**

 test = function (){
        $('#test_tutorial').modal('show').on('hidden.bs.modal', function (e) {
            $("#test_tutorial iframe").attr("src", "");
        }).on('show.bs.modal', function (e) {
            $("#test_tutorial iframe").attr("src", $("#test
    _tutorial iframe").attr("data-src"));
        });
    }
1

Late to the party, but some people may find this useful in the future. In my case I had a real struggle achieving this due to the video being embedded from a div with a video element in the iframe, not with the src directly in the iframe element, like this:

<iframe>
  <div class="videoContainer">
    <video src="#"></video>
  </div>
</iframe>

I solved this with the following approach:

$('#myModal').on('hidden.bs.modal', function() {
  $('iframe').contents().find('video')[0].pause();
});

It also pauses the video instead of refreshing the url/replacing the src attribute by the same value, as suggested in the accepted answer.

TNF
  • 199
  • 5
  • 17