sareed
sareed

Reputation: 780

YT getPlayerState() not a function

I need to access the YouTube states through JS to control a video slideshow. For some reason I am getting the getPlayerState() is not a function messages. I can access the playerState property directly though I would like to do it the 'correct' way as stated in the docs. Does anyone know what issue may be causing this?

Pertinent code:

if (window.YTIframeAPIReady) {
  ytPlayers[this.id] = new YT.Player(this.id, {
    events: {
      'onStateChange': youtubeStateChange,
    }
  }
}

function youtubeStateChange(event) {
  // This returns the player state.
  console.log(event.target.v.playerState);
  // This returns Uncaught TypeError: event.target.getPlayerState is not a function
  console.log(event.target.getPlayerState());
}

I have similar code in another part of the site that still seems to be working with the getPlayerState() so I am a bit stumped. Any help is greatly appreciated,

Upvotes: 1

Views: 5167

Answers (1)

Ian Venskus
Ian Venskus

Reputation: 952

You have do a check to see if getPlayerState is a function.

getPlayerState() is a number, which makes it even more fun!

For example:

if (typeof players[0].getPlayerState === "function") {
    console.log(event.target.getPlayerState());
}

Upvotes: 1

Related Questions