jpro jpro - 1 month ago 10
Javascript Question

YouTube iFrame API: no ready call, no error call

I'm trying to detect the "flash out of date" error in my code. The player object exists yet no callbacks are called.

var player = new YT.Player( 'pp-yt-player-div-id',
{
height: '50',
width: '50',
playerVars:
{
controls: 0,
showinfo: 0,
rel: 0
},
events:
{
onReady: function( event )
{
this.onReady();
}.bind( this ),
onStateChange: function( event )
{
this.onStateChange( event );
}.bind( this ),
onError: function( event )
{
console.log( 'on error: '+event );
this.onError( event );
}.bind( this )
}
} );


Is there a good way to detect the error in this scenario?

Answer

I don't understand why, but no error is called even though there is definitely an error. The only solution i can determine is a timeout hack which is obviously nasty.

var readyError = false;
var waitTimer = setTimeout( function()
{
    readyError = true;
    throw new Error( 'YouTube player not initializing. Possible Flash out of date error.' );
}, 10000 );

var ytPlayer = new YT.Player( 'youtube-player-id', {
  events: {
    onReady: function(e) {
      clearTimeout(waitTimer); 
    },
    onStateChange: function(e) {
      ... 
    },
    onError: function(e) {
      ... 
    } 
  } } );
Comments