Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Youtube Video Still Playing When Bootstrap Modal Closes

Tags:

I am creating a website with bootstrap that can be found here - http://www.branchingouteurope.com/digital-spark-testing/

If you select the video image you'll see a modal window open a youtube video. The problem I have is that when the modal window is closed the video keeps playing. I want this video to stop when the modal window is closed.

I've looked online and read many solutions however none seem to work. Here is the mark up...

<span class="main_video"> <div data-toggle="modal" data-target="#myModal" style="cursor:pointer"> <img src="img/master/video.fw.png" height="81%" width="60%" alt="Digital Spark Video"/> </div> </span>  <div class="modal-dialog"> <div class="modal-content">   <div class="modal-header">     <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>   </div>   <div class="modal-body">   <iframe width="100%" height="100%" src="//www.youtube.com/embed/sIFYPQjYhv8?rel=0" frameborder="0" allowfullscreen></iframe>   </div> </div> 

`

like image 670
Stephen Tamlin Avatar asked Mar 24 '14 15:03

Stephen Tamlin


2 Answers

Here's a little bit lighter of an answer based on dizarter's version and one of the solutions he links to.

$('#modal-video').on('hidden.bs.modal', function () {     $("#modal-video iframe").attr("src", $("#modal-video iframe").attr("src")); }); 
like image 51
Cloudkiller Avatar answered Sep 21 '22 15:09

Cloudkiller


Testing here in FireFox 26.0, works as expected. When I either close the Modal or click outside of it and then re-open it - video is back to start and stopped.

EDIT 1

Reproduced in Chrome, the video indeed keeps playing after the Modal is closed.

Try these already answered questions

Stop a youtube video with jquery?

Twitter Bootstrap Modal stop Youtube video

The best way seems to be to use YouTube API to stop the video. Answer that uses this method is available in the questions above.

EDIT 2

This solution seems to be working.

First, get the JS from this post: YouTube iframe API: how do I control a iframe player that's already in the HTML? and include it on the page.

Add this JS after you have loaded the above script (just before the closing body tag)

<script type="text/javascript">     $('#myModal').on('hidden.bs.modal', function () {         callPlayer('yt-player', 'stopVideo');     }); </script> 

You will also need to add an ID to the div containing the iframe, as below

<div class="modal-body" id="yt-player">     <iframe src="//www.youtube.com/embed/sIFYPQjYhv8?rel=0&enablejsapi=1" allowfullscreen="" width="100%" frameborder="0" height="100%"></iframe> </div> 
like image 36
dizarter Avatar answered Sep 20 '22 15:09

dizarter