I can't seem to get a YouTube video to stop playing when a modal is closed. The video goes away but the audio continues playing.
I've tried the solutions in many other questions but nothing seems to work. I think it's probably because my video is deeply nested and I haven't been able to target it. And/or because the iframes have element IDs of the same names. (Don't get me started.)
Here's a simplified look at the DOM:
html
head
body
<div id="popoverview">
<div id="popoveritem">
<iframe id="popoverframe"> <!-- this is our modal -->
#document
html
head
body
<div id="popovercontainer">
<div id="popover_upc_video">
<div id="current_video">
<div id="player">
<iframe id="popoverframe">
#document <!-- this is from youtube -->
html
head
body
<div id="player">
<div id="playercontainer">
<embed id="video-player-flash"> <!-- the video -->
Here's a bit from the javascript inserting the youtube video--including ?enablejsapi=1 --into the modal:
document.getElementById('player').innerHTML = '<iframe id="youtubevideo" width="620" height="376" src="http://www.youtube.com/embed/'+youtube_id+'?enablejsapi=1?hl=en&rel=0&fs=1&modestbranding=1" frameborder="0" allowfullscreen>Error?</iframe>';
Here's a bit from the function that closes the modal. Problem is that the audio keeps playing. The error message is Uncaught TypeError: Object [object Object] has no method 'stopVideo'.
var myPlayer = $('#player > #video-player-flash');
myPlayer.stopVideo();
I've tried quite a few methods of targeting but nothing is working.
Thanks for the help. Charlie Magee