Created
May 30, 2014 19:23
-
-
Save ariaBennett/1d4fe4b1c7aa8ab75d68 to your computer and use it in GitHub Desktop.
youtube iframe api integration with meteor
This file contains hidden or 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
if (Meteor.isClient) { | |
Meteor.startup(function(){ | |
// Declare g2 namespace. | |
window.g2 = {}; | |
// Declare he parse object on g2 | |
g2.parse = {}; | |
// parse a given url and return a data object | |
g2.parse.url = function(url) { | |
var data = {} | |
data.playerType = (function() { | |
var workingSlice = url.slice(url.indexOf('.') + 1); | |
var workingSlice = workingSlice.slice(0, workingSlice.indexOf('.')); | |
return workingSlice; | |
})(); | |
return data; | |
} | |
// Declare get on g2. | |
g2.get = {}; | |
// Get the player container div. | |
g2.get.player = function() { | |
var contents = g2.get.container.player().children; | |
if (contents.length !== 0) { | |
var data = g2.parse.url(contents[0].src); | |
data.contents = g2.get.container.player().children[0]; | |
return data; | |
} else { | |
return null; | |
} | |
}; | |
// Declare container on g2.get. | |
g2.get.container = {}; | |
// Get the player container div. | |
g2.get.container.player = function() { | |
return document.getElementById('playerContainer'); | |
}; | |
// Declare video on g2. | |
g2.video = {}; | |
// Declare size on g2.video. | |
g2.video.size = {}; | |
// Set the video's size. | |
g2.video.size.width = '100%'; | |
g2.video.size.height = '100%'; | |
// Clears video players from player div. | |
g2.video.clear = function() { | |
var playerContainer = g2.get.container.player(); | |
// clear player's content | |
playerContainer.innerHTML = ""; | |
}; | |
// Insert a new video and play it. | |
g2.video.add = function(data) { | |
g2.video.clear(); | |
var origin = 'http://localhost:3000'; | |
var playerContainer = g2.get.container.player(); | |
if (data.player === 'youtube') { | |
// Adds the iframe for the youtube player | |
playerContainer.innerHTML = '<iframe id="youtubePlayer" type="text/html"' + | |
'width=' + g2.video.size.width + ' ' + | |
'height=' + g2.video.size.height + ' ' + | |
'src="http://www.youtube.com/embed/' + data.id + | |
'?autoplay=1&enablejsapi=1' + | |
'" frameborder="0"></iframe>'; | |
} | |
window.player = new YT.Player('youtubePlayer', { | |
events: { | |
'onReady': onPlayerReady, | |
'onStateChange': onPlayerStateChange | |
} | |
}); | |
} | |
g2.video.seek = function(time) { | |
}; | |
g2.initialize = function() { | |
var tag = document.createElement('script'); | |
tag.src = "https://www.youtube.com/iframe_api"; | |
var firstScriptTag = document.getElementsByTagName('script')[0]; | |
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); | |
window.onPlayerReady = function(event) { | |
} | |
window.done = false; | |
window.onPlayerStateChange = function(event) { | |
} | |
window.stopVideo = function() { | |
player.stopVideo(); | |
} | |
window.onYouTubeIframeAPIReady = function() { | |
} | |
} | |
g2.initialize(); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment