Skip to content

Instantly share code, notes, and snippets.

@gabrielhpugliese
Created September 7, 2013 21:17
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save gabrielhpugliese/6479387 to your computer and use it in GitHub Desktop.
Save gabrielhpugliese/6479387 to your computer and use it in GitHub Desktop.
Snippet for using Google Youtube APIs with Meteor rendering engine (not Meteor UI). Assume the template is called "channel". You only need to implement onPlayerReady of line 44. It uses jQuery.getScript();
Meteor.startup(function () {
Session.set('YTApiReady', false);
Session.set('channelRendered', false);
});
onYouTubeIframeAPIReady = function() {
Session.set('YTApiReady', true);
};
Template.channel.created = function () {
if (typeof player === 'undefined')
$.getScript('https://www.youtube.com/iframe_api', function () {});
};
Template.channel.rendered = function() {
Session.set('channelRendered', true);
};
Template.channel.destroyed = function () {
Session.set('channelRendered', false);
};
Deps.autorun(function (c) {
if (Session.equals('YTApiReady', false) ||
Session.equals('channelRendered', false)) {
return;
}
var interval = Meteor.setInterval(function () {
if(!document.getElementById('player-div')) {
return;
}
var playerDiv = document.createElement('div'),
channel = Template.channel.getChannel();
playerDiv.id = 'player-div';
document.getElementById('player-parent').innerHTML = '';
document.getElementById('player-parent').appendChild(playerDiv);
player = null;
player = new YT.Player('player-div', {
videoId: channel.URL,
events: {
'onReady': onPlayerReady
}
});
Meteor.clearInterval(interval);
}, 100);
});
@neilsmind
Copy link

Thank you for posting this. It's been a HUGE help for an app I'm writing. Would you mind sharing the getChannel() function that you're using to set the videoId?

@bobmonsour
Copy link

Neil, I would expect that the getChannel() function does a lookup into the mongo collection that contains whatever metadata he's maintaining each video. I am borrowing this code too and I have a collection with the metadata for each video that I want to play. I hope that helps.

@gabrielhpugliese
Copy link
Author

Yes, it's a channel on mongodb

@JamesLefrere
Copy link

You have no idea how much time you just saved me. Thanks!

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