Working on YouTube playback

This commit is contained in:
Sam Potts 2015-04-15 23:50:03 +10:00
parent d04b278802
commit 3d1a586314
5 changed files with 101 additions and 17 deletions

2
dist/plyr.css vendored

File diff suppressed because one or more lines are too long

2
dist/plyr.js vendored

File diff suppressed because one or more lines are too long

View File

@ -9,6 +9,7 @@
(function (api) { (function (api) {
"use strict"; "use strict";
/*global YT*/
// Globals // Globals
var fullscreen, config; var fullscreen, config;
@ -50,6 +51,7 @@
classes: { classes: {
video: "player-video", video: "player-video",
videoWrapper: "player-video-wrapper", videoWrapper: "player-video-wrapper",
embedWrapper: "player-video-embed",
audio: "player-audio", audio: "player-audio",
stopped: "stopped", stopped: "stopped",
playing: "playing", playing: "playing",
@ -82,6 +84,9 @@
}, },
controls: ["restart", "rewind", "play", "fast-forward", "current-time", "duration", "mute", "volume", "captions", "fullscreen"], controls: ["restart", "rewind", "play", "fast-forward", "current-time", "duration", "mute", "volume", "captions", "fullscreen"],
onSetup: function() {}, onSetup: function() {},
youtube: {
regex: /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/
}
}; };
// Build the default HTML // Build the default HTML
@ -330,6 +335,18 @@
return false; return false;
} }
// Inject a script
function _injectScript(source) {
if(document.querySelectorAll("script[src='" + source + "']").length) {
return;
}
var tag = document.createElement("script");
tag.src = source;
var firstScriptTag = document.getElementsByTagName("script")[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
}
// Element exists in an array // Element exists in an array
function _inArray(haystack, needle) { function _inArray(haystack, needle) {
return Array.prototype.indexOf && (haystack.indexOf(needle) != -1); return Array.prototype.indexOf && (haystack.indexOf(needle) != -1);
@ -821,6 +838,14 @@
// Cache the container // Cache the container
player.videoContainer = wrapper; player.videoContainer = wrapper;
// YouTube
var firstSource = player.media.querySelectorAll("source")[0],
matches = firstSource.src.match(config.youtube.regex);
if(firstSource.type == "video/youtube" && matches && matches[2].length == 11) {
_setupYouTube(matches[2]);
}
} }
} }
@ -830,6 +855,48 @@
} }
} }
// Setup YouTube
function _setupYouTube(id) {
player.embed = true;
// Hide the <video> element
player.media.style.display = "none";
// Create the YouTube iframe
var iframe = document.createElement("iframe");
iframe.src = "https://www.youtube.com/embed/"+ id + "?rel=0&vq=hd720&iv_load_policy=3&controls=0&autoplay=0&showinfo=0&wmode=transparent&?enablejsapi=1";
iframe.id = "youtube" + Math.floor(Math.random() * (10000));
// Add embed class for responsive
_toggleClass(player.videoContainer, config.classes.embedWrapper, true);
// Append the iframe
player.videoContainer.appendChild(iframe);
// Add the API
_injectScript("https://www.youtube.com/iframe_api");
// Setup callback for the API
window.onYouTubeIframeAPIReady = function() {
_log("YouTube API Ready");
_log(iframe.id);
_log(id);
player.youtube = new YT.Player(iframe.id, {
events: {
onReady: function() {
console.log("ready");
},
onStateChange: function(e) {
console.log(e);
}
}
});
_log(player.youtube);
}
}
// Setup captions // Setup captions
function _setupCaptions() { function _setupCaptions() {
if(player.type === "video") { if(player.type === "video") {
@ -1558,7 +1625,9 @@
_setupAria(); _setupAria();
// Captions // Captions
_setupCaptions(); if(!player.embed) {
_setupCaptions();
}
// Set volume // Set volume
_setVolume(); _setVolume();
@ -1570,17 +1639,6 @@
_listeners(); _listeners();
} }
var plugins = Object.keys(api.plugins);
for (var i = plugins.length - 1; i >= 0; i--) {
var key = plugins[i];
_log("Setting up " + key + " plugin");
// Call setup and pass plyr instance
api.plugins[key].setup.apply(player);
}
// Successful setup // Successful setup
return true; return true;
} }
@ -1679,7 +1737,4 @@
return players; return players;
} }
// Setup plugins
api.plugins = {};
}(this.plyr = this.plyr || {})); }(this.plyr = this.plyr || {}));

View File

@ -12,6 +12,20 @@
setup: function() { setup: function() {
console.log("Setup youtube"); console.log("Setup youtube");
console.log(this); console.log(this);
var player = this;
// Find child <source> elements
var sources = player.media.querySelectorAll("source");
// Remove each
for (var i = sources.length - 1; i >= 0; i--) {
var source = sources[i];
if(source.type == "video/youtube") {
console.log(source.src);
}
}
} }
}; };

View File

@ -147,6 +147,21 @@
vertical-align: middle; vertical-align: middle;
} }
// For embeds
&-video-embed {
padding-bottom: 56.25%; /* 16:9 */
height: 0;
iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
border: 0;
}
}
// Captions // Captions
&-captions { &-captions {
display: none; display: none;