var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
$("iframe").each(function(indx){
var min = $(this)[0].src.indexOf('?');
var src = $(this)[0].src.substr(0, min) + '?enablejsapi=1';
$(this)[0].src = src;
$(this)[0].id = 'player' + indx;
});
function onPlayerStateChange(event) {
var flag = 0;
$(players).each(function(){
if(this.getPlayerState() == 1){
flag += 1;
}
});
if(event.data == 1 && flag > 1){
stop_all();
event.target.playVideo();
}
}
function stop_all (){
$(players).each(function(){
this.pauseVideo();
});
}
var players = [];
function onYouTubeIframeAPIReady() {
$("iframe").each(function(indx){
var temp = new YT.Player('player' + indx, {
events: {
'onStateChange': onPlayerStateChange
}
});
players.push(temp);
});
}
не забудьте подключить jQuery