Skip to content

Instantly share code, notes, and snippets.

@egermano
Created May 13, 2014 14:38
Show Gist options
  • Save egermano/d41ab15b03c3455870c4 to your computer and use it in GitHub Desktop.
Save egermano/d41ab15b03c3455870c4 to your computer and use it in GitHub Desktop.
YT Embed API events implements
var thePlayer = new YT.Player('yt-embed', {
height: '360',
width: '540',
videoId: 'bScJ8bbwMNM',
events: {
'onStateChange': function(e){
switch (e.data) {
case YT.PlayerState.ENDED:
clearWatcherVideo();
break;
case YT.PlayerState.PLAYING:
watcherVideo();
break;
case YT.PlayerState.PAUSED:
clearWatcherVideo();
break;
case YT.PlayerState.BUFFERING:
break;
case YT.PlayerState.CUED:
break;
}
}
}})
function logVideoPercentual() {
var total = thePlayer.getDuration(),
current = thePlayer.getCurrentTime();
console.log('percentual played', (current/total) * 100);
}
function watcherVideo () {
window.videoInterval = setInterval(function(){
logVideoPercentual();
}, 1000);
}
function clearWatcherVideo() {
clearInterval(window.videoInterval);
logVideoPercentual();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment