I have a Vimeo iframe video in Bootstrap video. I need to have it start playing when I trigger modal and stop playing when a modal is closed. Currently I can have in start playing on modal open by having iframe with no src attribute and having it filled with jQuery on triggering modal. This is the code snippet;
jQuery("#videogumb").click(function() {
jQuery('#myModal .modal-body iframe').attr('src','the-source-code');
});
This works fine and I get it to start when modal opens but when I close modal the music keeps playing in the background.
The other method works fine when but only for closing the modal without having it set to autoplay. So the autoplay is set to 0 and I have this snippet;
jQuery(".modal-backdrop, #myModal .close, #myModal .btn").live("click", function() {
jQuery("#myModal iframe").attr("src", jQuery("#myModal iframe").attr("src"));
});
This stops the video when I close modal. I need to have this combined somehow. I need to have it autoplay when modal opens and to stop playing when modal closes.
Any clues?
Thanks.
When you close the model, it does not call the player to stop playing the video. You can you add onclick="javascript::player. api('pause')" in your model close button. It should resolve your issue.
When using Bootstrap 3 modal windows normally the content is there, in the same window, so, whenever you close a modal, you just hide it within the same page. If it's a <video> , it'll keep playing until you refresh the page, or re-open the modal just to pause it (since it can't be stopped).
To prevent closing bootstrap modal when click outside of modal window we need add properties data-backdrop="static" and data-keyboard="false" to button which we are using to show modal window like as shown following.
To add to eroedig's answer, check out Vimeo's documentation Calling the API with Froogaloop.
<iframe src="//player.vimeo.com/video/116160160?api=1&player_id=vimeoplayer&title=0&byline=0&portrait=0" name="vimeoplayer" id="nofocusvideo" width="615" height="346" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
var iframe = document.getElementById('nofocusvideo');
<script src="http://a.vimeocdn.com/js/froogaloop2.min.js"></script>
<script>
var iframe = document.getElementById('nofocusvideo');
// $f == Froogaloop
var player = $f(iframe);
$('.modal').on('hidden.bs.modal', function () {
player.api('pause');
})
$('.modal').on('shown.bs.modal', function () {
player.api('play');
})
</script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With