From 3d1a586314c14fcb5c78060f14efe9f9774ce8dd Mon Sep 17 00:00:00 2001 From: Sam Potts Date: Wed, 15 Apr 2015 23:50:03 +1000 Subject: Working on YouTube playback --- src/js/plyr.js | 85 +++++++++++++++++++++++++++++++++++++++++--------- src/js/plyr.youtube.js | 14 +++++++++ src/less/plyr.less | 15 +++++++++ 3 files changed, 99 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/js/plyr.js b/src/js/plyr.js index dae27890..865bb9a9 100644 --- a/src/js/plyr.js +++ b/src/js/plyr.js @@ -9,6 +9,7 @@ (function (api) { "use strict"; + /*global YT*/ // Globals var fullscreen, config; @@ -50,6 +51,7 @@ classes: { video: "player-video", videoWrapper: "player-video-wrapper", + embedWrapper: "player-video-embed", audio: "player-audio", stopped: "stopped", playing: "playing", @@ -82,6 +84,9 @@ }, controls: ["restart", "rewind", "play", "fast-forward", "current-time", "duration", "mute", "volume", "captions", "fullscreen"], onSetup: function() {}, + youtube: { + regex: /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/ + } }; // Build the default HTML @@ -330,6 +335,18 @@ 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 function _inArray(haystack, needle) { return Array.prototype.indexOf && (haystack.indexOf(needle) != -1); @@ -821,6 +838,14 @@ // Cache the container 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