Skip to content

Instantly share code, notes, and snippets.

@myungseokang
Created April 25, 2017 07:23
Show Gist options
  • Save myungseokang/1083ed530c826670f6c244616ee9283d to your computer and use it in GitHub Desktop.
Save myungseokang/1083ed530c826670f6c244616ee9283d to your computer and use it in GitHub Desktop.
Youtube player with loop using Iframe API
<script>
// Inject this code
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
videoId: 'WANTED_VIDEO_ID',
events: {
'onStateChange': onPlayerStateChange
}
});
}
function onPlayerStateChange(e) {
if (e.data === YT.PlayerState.ENDED) {
player.playVideo();
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment