Skip to content

Instantly share code, notes, and snippets.

@zishe
Created March 1, 2018 20:43
Show Gist options
  • Save zishe/2ef5e495efa183fd6b3f2c16b3760af9 to your computer and use it in GitHub Desktop.
Save zishe/2ef5e495efa183fd6b3f2c16b3760af9 to your computer and use it in GitHub Desktop.
players = [];
function onYouTubeIframeAPIReady() {
_.each($('.article__video-frame'), function(item, i) {
var player = new YT.Player('player' + i, {
height: '400',
width: '700',
videoId: $(item).attr('data-vid'),
events: {
'onStateChange': onPlayerStateChange
}
});
players.push(player);
});
}
function onPlayerStateChange(event) {
console.log(event);
if (event.data == YT.PlayerState.PLAYING) {
stopVideo(event.target.a.id);
}
}
function stopVideo(player_id) {
_.each(players, function(player, i) {
if (player_id !== "player" + i) {
players[i].stopVideo();
}
});
}
function loadScript() {
var tag = document.createElement('script');
tag.src = "//www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
}
loadScript();
$(document).ready(function() {
setTimeout(function(){
onYouTubeIframeAPIReady();
}, 300);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment