I have a training program where I'm trying to display a YouTube video in an iFrame, automatically play it, and advance to the next page when the video completes. I can get the onYouTubeIframeAPIReady function to fire, but none of the other events will. Actually, I'm only interested in state change.
var tag = document.createElement('script');
tag.src = 'https://www.youtube.com/iframe_api';
tag.id = 'apiScript';
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
...
//function onYouTubeIframeAPIReady() { //tried this too but no change
window.onYouTubeIframeAPIReady = function() {
player = new YT.Player('externalComponent', {
events: {
'onReady': onPlayerReady,
'onError': onPlayerError,
'onStateChange': onPlayerStateChange
}
});
console.log('api ready');
}
function onPlayerStateChange(event) {
console.log('onStateChange');
console.log(YT.PlayerState);
}
When it runs, you can see "api ready" in the console but nothing else. iFrame looks like:
<iframe id="externalComponent" src="https://youtube.com/embed/_S_Bg9qfa8o?enablejsapi=1&controls=0&autoplay=1" width="1024" height="576" allow="autoplay" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
What is stopping onStateChange from working and not automatically playing? The controls do not show.