Created
August 27, 2014 21:11
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This has helped me, thanks a lot!