We have a Facebook Tab application (probably not relevant) that contains a YouTube video.
Everything works fine on the first occasion the page is loaded from an empty cache. On subsequent page loads, however, once the video stops loading, the onStateChange
handler simply isn't firing in Internet Explorer.
Everything works fine in Chrome and Firefox, this problem only seems to affect IE and, weirdly, only occurs from a non-empty cache. It looks very similar to gdata-issues #2942, except that issue has Status: Fixed
.
The relevant chunks of our code are as follows:
Page
<!DOCTYPE html>
...
<div class="player-surround">
<div id="ytplayer">
You need Flash player 9+ and JavaScript enabled to view this video.
</div>
</div>
...
<script src="//ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js"></script>
<script type="text/javascript">
var params = { allowScriptAccess: "always" };
var atts = { id: "ytplayer" };
// YouTube video ID redacted here
swfobject.embedSWF("http://www.youtube.com/v/XXXXXXXXXXXXXX?enablejsapi=1&playerapiid=ytplayer&version=3",
"ytplayer", "587", "330", "8", null, null, params, atts);
</script>
<script src="//www.youtube.com/player_api"></script>
Included JavaScript
MFA.onPlayerStateChange = function(evt) {
var state = this.getPlayerState();
log('158: STATE CHANGED: ' + state);
// Then do some stuff, but the `log` call above doesn't fire
}
MFA.getPlayerState = function() {
var playerState;
if (this.ytplayer) {
playerState = this.ytplayer.getPlayerState();
switch (playerState) {
case 5: return 'video cued';
case 3: return 'buffering';
case 2: return 'paused';
case 1: return 'playing';
case 0: return 'ended';
case -1: return 'unstarted';
default: return 'Status uncertain';
}
}
};
(Obviously, I've missed a bunch of unrelated code out from this issue.)
Does anyone have any ideas why everything would work fine, unless IE has seen the page before? I could understand if it never worked in IE, but it works ok when the cache is empty, but fails when you reload the page.
All ideas gratefully received.
Edit
Greg Schechter pointed out that my code sample uses the swfobject.embedSWF
code, rather than the iframe embed code.
We did originally use the iframe embed code:
var s = {
playerHeight: '330',
playerWidth: '587',
playerVars: { 'rel': 0 },
videos: {
stage1: 'XXXXXXXXXXXXXX', // ...
}
};
window.onYouTubePlayerAPIReady = function() {
MFA.ytplayer = new YT.Player('ytplayer', {
height: s.playerHeight,
width: s.playerWidth,
videoId: s.videos.stage1,
playerVars: s.playerVars,
events: {
'onReady': $.proxy(MFA.onPlayerReady, MFA),
'onStateChange': $.proxy(MFA.onPlayerStateChange, MFA),
'onError': $.proxy(MFA.onPlayerError, MFA)
}
});
}
, but this was exhibiting the same problem. The swfobject.embedSWF
code was added in subsequently as an attempt to fix the problem, as we have previously seen problems with IE being over-paranoid with API access across domains from iframes.