Instead of using iframes, you really want to use the iFrame Player API. Depending on how many videos you really wanted to embed, you can make this javascript more dynamic, but this working example should give you enough to get you started.
Basically what I'm doing here is initializing two players and pausing the opposite video when a playerโs state changes to a game.
You can play with the following code at http://jsfiddle.net/mattkoskela/Whxjx/
<script> var tag = document.createElement('script'); tag.src = "//www.youtube.com/iframe_api"; var firstScriptTag = document.getElementsByTagName('script')[0]; firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); var player; function onYouTubeIframeAPIReady() { player1 = new YT.Player('player1', { height: '293', width: '520', videoId: 'zXV8GMSc5Vg', events: { 'onStateChange': onPlayerStateChange } }); player2 = new YT.Player('player2', { height: '293', width: '520', videoId: 'LTy0TzA_4DQ', events: { 'onStateChange': onPlayerStateChange } }); } function onPlayerStateChange(event) { if (event.data == YT.PlayerState.PLAYING) { stopVideo(event.target.a.id); } } function stopVideo(player_id) { if (player_id == "player1") { player2.stopVideo(); } else if (player_id == "player2") { player1.stopVideo(); } }
source share