Skip to content

Instantly share code, notes, and snippets.

@tiaozi0912
Created August 27, 2014 21:11
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tiaozi0912/76019104a97b13739a91 to your computer and use it in GitHub Desktop.
Save tiaozi0912/76019104a97b13739a91 to your computer and use it in GitHub Desktop.
Solution for the Youtube Iframe API Firefox issue that player.playVideo() doesn't work
var playerConfig = {}, // Define the player config here
queue = { // To queue a function and invoke when player is ready
content: null,
push: function(fn) {
this.content = fn;
},
pop: function() {
this.content.call();
this.content = null;
}
},
player;
window.onYouTubeIframeAPIReady = function() {
player = new YT.Player('player', {
videoId: 'player',
playerVars: playerConfig,
events: {
onReady: onPlayerReady
}
});
};
// API event: when the player is ready, call the function in the queue
function onPlayerReady() {
if (queue.content) queue.pop();
}
// Helper function to check if the player is ready
function isPlayerReady(player) {
return player && typeof player.playVideo === 'function';
}
// Instead of calling player.playVideo() directly,
// using this function to play the video.
// If the player is not ready, queue player.playVideo() and invoke it when the player is ready
function playVideo(player) {
isPlayerReady(player) ? player.playVideo() : queue.push(function() {
player.playVideo();
});
}
@Eliyar
Copy link

Eliyar commented Dec 31, 2016

This has helped me, thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment