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.
I tried using froogaloop2 but it always returned this error: Cannot read property 'ready' of undefined
Instead you can simply use
postMessage
to callplay
andpause
on the video inside theiframe
.I accomplished this by calling both the Bootstrap Modal API and Vimeo API, code below. You will need to include Vimeo's Froogaloop script as well, also below.
To add to eroedig's answer, check out Vimeo's documentation Calling the API with Froogaloop.
Refer to Vimeo's embedding documentation.
There is an attribute that you can set called
autoplay
. They note that it might not work on some devices, but setting this attribute to1
(or maybetrue
) should help.The code might look something like this:
So I tried all the suggestions here and nothing was working quite as well as the updated documentation from Vimeo .. https://github.com/vimeo/player.js#pause-promisevoid-error