This is an example of handling start and stop events:
HTML file (index.html):
<!DOCTYPE html>
<html>
<head>
<title>Stackoverflow</title>
<script type="text/javascript" src="http://www.youtube.com/player_api"> </script>
<script type="text/javascript" src="sof.js"> </script>
</head>
<body>
<div id="player"></div>
</body>
</html>
And JavaScript (sof.js):
var player;
function onYouTubePlayerAPIReady() {
player = new YT.Player('player', {
height: '390',
width: '640',
videoId: 'u1zgFlCw8Aw',
events: {
'onStateChange': function (event) {
switch (event.data) {
case -1:
console.log ('unstarted');
break;
case 0:
console.log ('ended');
break;
case 1:
console.log ('playing');
break;
case 2:
console.log ('paused');
break;
case 3:
console.log ('buffering');
break;
case 5:
console.log ('video cued');
break;
}
}
}
});
}
For each case, you can install a handler.
For more information:
source
share