Last active
March 23, 2017 16:58
-
-
Save mflisikowski/550059ba53010fb8662e0d1763aa6706 to your computer and use it in GitHub Desktop.
Example of use Youtube API Iframe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var player; | |
var videoID = 'Flze-rwT7lM'; // https://youtu.be/Flze-rwT7lM | |
var videoPlayerPlaceholder = 'youtubePlayer'; | |
var youtubePlayerInit = function() { | |
if ((typeof YT !== "undefined") && YT && YT.Player && videoID) { | |
player = new YT.Player(videoPlayerPlaceholder, { | |
videoId: videoID, | |
playerVars: { | |
'autoplay': 0, | |
'rel': 0, | |
'showinfo': 0 | |
}, | |
events: { | |
'onReady': youtubePlayerReady, | |
'onStateChange': youtubePlayerState | |
} | |
}); | |
} else { | |
setTimeout(youtubePlayerInit, 100); | |
} | |
}; | |
var youtubePlayerReady = function(e) { | |
console.log('ready: ' + e.data); | |
}; | |
var youtubePlayerState = function(e) { | |
if (e.data == YT.PlayerState.ENDED) { | |
if (/localhost|front.end/.test(document.location.href)) console.log('PlayerState: ENDED'); | |
} | |
if (e.data == YT.PlayerState.UNSTARTED) { | |
if (/localhost|front.end/.test(document.location.href)) console.log('PlayerState: UNSTARTED'); | |
} | |
if (e.data == YT.PlayerState.PLAYING) { | |
if (/localhost|front.end/.test(document.location.href)) console.log('PlayerState: PLAYING'); | |
} | |
if (e.data == YT.PlayerState.PAUSED) { | |
if (/localhost|front.end/.test(document.location.href)) console.log('PlayerState: PAUSED'); | |
} | |
if (e.data == YT.PlayerState.BUFFERING) { | |
if (/localhost|front.end/.test(document.location.href)) console.log('PlayerState: BUFFERING'); | |
} | |
if (e.data == YT.PlayerState.CUED) { | |
if (/localhost|front.end/.test(document.location.href)) console.log('PlayerState: CUED'); | |
} | |
}; | |
youtubePlayerInit(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment