YouTube iframe player API - OnStateChange not firing
Asked Answered
A

3

17

I have literally just copy & pasted the code from the YouTube developer page YouTube Player API Reference for iframe Embeds (from underneath the heading "Getting Started"). The only difference, is that I added an alert to fire when the state changed, because I thought I was doing something wrong within the onPlayerStateChange function.

You can see the jsFiddle at http://jsfiddle.net/jesMv/.

As stated, it's just an exact copy of the code from the YouTube developer page with the added

alert('State Changed')

as the first thing to fire in the onPlayerStateChange function.

Nothing is happening, however... No matter how I look at this and what I change, I simply can't get the onStateChange to do anything.

How can I fix this problem?

Acridine answered 13/6, 2013 at 2:1 Comment(0)
E
25

There was a temporary issue with the iFrame Player API (which was fixed in June 2013) that you can read about here: https://code.google.com/p/gdata-issues/issues/detail?id=4706

Jeff Posnick posted a temporary workaround here: http://jsfiddle.net/jeffposnick/yhWsG/3/

As a temporary fix, you just need to add the event listener within the onReady event:

function onReady() {
    player.addEventListener('onStateChange', function(e) {
        console.log('State is:', e.data);
    });
}

Make sure to remove the onStateChange event from the YT.PLAYER constructor (see the jsfiddle).

Also, as someone mentioned on the Google Code Issue Thread, you set an interval and poll the player for its current state instead of listening for the onStateChange event. Here is an example code snippet for doing that:

setInterval( function() {
  var state = player.getPlayerState();
  if ( playerState !== state ) {
    onPlayerStateChange( {
      data: state
    });
  }
}, 10);

Firefox and IE Issues

Other people have mentioned that Firefox will not instantiate the YouTube Player if it is placed in a container with the css property display: none. Internet Explorer will also not work with visibility: hidden. If you're finding this to be the issue, try positioning the container off the page with something like left: -150%.

Steve Meisner talks about this here: YouTube API does not appear to load in Firefox, IFrame gets loaded , but the onPlayerReady event never fires?

And another related SO question: YouTube iframe API - onReady and onStateChanged events not firing in IE9

Edit: I've edited this answer to be more thorough because people are still seeing this error after the original bug was fixed in 2013.

Exotoxin answered 13/6, 2013 at 2:11 Comment(10)
The issue seems now that I write not to be fixed (PT time 00.44) yet.Bergen
May not be working if you're using an iframe rather than a div that gets replaced once the api loadsHalstead
In my case, this was not the solution (and this bug should be resolved by now?). I had display:none on the container of the video and the API didn't fire. More here: https://mcmap.net/q/590888/-youtube-api-does-not-appear-to-load-in-firefox-iframe-gets-loaded-but-the-onplayerready-event-never-firesRattletrap
In 2014, I've seen this break and mysteriously start working again about 30 minutes later. The fix given in this answer didn't work for me while it was broken.Polemoniaceous
I've also seen this issue following the bug fix mentioned. For me it occurs when adding the player DOM node via JS after a user interaction. Unfortunately, it happens only about 10% of the time so it's difficult to troubleshoot.Ludendorff
@Ludendorff interesting, let me know if you end up figuring it out and we can add it to the answer. People are definitely still seeing this issue so I want the answer to be as thorough as possible. Thanks!Exotoxin
Thanks! I needed the polling workaround to get this working... Two updates to the polling function (for completeness' sake): - Add var playerState as a first line, to initialize the variable - Add the line playerState = state, to keep track of the new state once the event has fired.Cotswold
When I used the <iframe> instead of the <div id="player"> the onStateChange only fired sometimes and not others, as if there were an order of operations issue. It worked every time when I just used the <div id="player">.Mamey
This issues still occurs for me as of this writing. The polling solution above using setInterval works though.Milan
it doesn't help me with iframeFiord
W
1

onStateChange does not work in any version of Internet Explorer or Edge. I assume it will begin working once Microsoft moves Edge over to being Chromium-based. But whether IE9, 11, or Edge, I cannot get this event to fire, even when it fires cleanly in Chrome, identical code.

Winding answered 8/8, 2019 at 15:6 Comment(0)
M
0

This might help someone (likely future me) stumbling across this.

I guess they assume you're going to use the API to create the <iframe> element that you want to control, but this doesn't work well if you are, for example, using a modern frontend framework like React to have multiple switchable iframes.

If you're trying to hook up the API and happen to be starting with an <iframe> created elsewhere, make sure the src includes the param enablejsapi=1.

or the iframe has an enablejsapi="true" attribute (doesn't work, good docs, Google).

This is buried in the docs and took me longer to figure out than it shoudl've.

Masochism answered 11/4, 2023 at 18:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.