From 4cbd302d1ea7c4b315228706b7a942f2635142d8 Mon Sep 17 00:00:00 2001 From: Alex Wilde Date: Thu, 16 Jul 2015 18:43:47 +0200 Subject: [PATCH 1/3] Use an existing player if present when video changes. See original issue https://github.com/brandly/angular-youtube-embed/issues/46 Fix adapted from https://github.com/brandly/angular-youtube-embed/pull/48 --- src/angular-youtube-embed.js | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/src/angular-youtube-embed.js b/src/angular-youtube-embed.js index 514c7c2..97dfd58 100644 --- a/src/angular-youtube-embed.js +++ b/src/angular-youtube-embed.js @@ -186,13 +186,34 @@ angular.module('youtube-embed', ['ng']) } function loadPlayer () { - if (scope.videoId || scope.playerVars.list) { - if (scope.player && typeof scope.player.destroy === 'function') { - scope.player.destroy(); + // If present, use an existing player + if(scope.player && scope.player.getIframe()) { + var playerVars = ( playerVars ? playerVars : angular.copy(scope.playerVars) ); + playerVars.start = playerVars.start || scope.urlStartTime; + playerVars.end = playerVars.end || scope.urlEndTime; + + // Use cueVideoById() instead of loadVideoById() so the video doesn't + // start to play automatically. + if (scope.videoId) { + scope.player.cueVideoById({ + videoId:scope.videoId, + startSeconds:playerVars.start, + endSeconds:playerVars.end + }); } - + else if(scope.playerVars.list) { + scope.player.cuePlaylist({ + playlist:scope.playerVars.list, + startSeconds:playerVars.start, + endSeconds:playerVars.end + }); + } + } + // Otherwise, create a new player + else if(scope.videoId || scope.playerVars.list) { scope.player = createPlayer(); } + }; var stopWatchingReady = scope.$watch( From 2cd11ffe1b9813dc07fa9f1282496a154f893c6e Mon Sep 17 00:00:00 2001 From: Alex Wilde Date: Thu, 16 Jul 2015 20:34:02 +0200 Subject: [PATCH 2/3] Added extra check on scope.player. --- src/angular-youtube-embed.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/angular-youtube-embed.js b/src/angular-youtube-embed.js index 97dfd58..18b2b58 100644 --- a/src/angular-youtube-embed.js +++ b/src/angular-youtube-embed.js @@ -184,10 +184,18 @@ angular.module('youtube-embed', ['ng']) player.id = playerId; return player; } + + function isEmpty (obj) { + if (obj == null) return true; + if (Object.keys) return Object.keys(obj).length == 0; + var keys = []; + for (var key in obj) if (_.has(obj, key)) keys.push(key); + return keys.length == 0; + }; function loadPlayer () { // If present, use an existing player - if(scope.player && scope.player.getIframe()) { + if(scope.player && !isEmpty(scope.player) && scope.player.getIframe()) { var playerVars = ( playerVars ? playerVars : angular.copy(scope.playerVars) ); playerVars.start = playerVars.start || scope.urlStartTime; playerVars.end = playerVars.end || scope.urlEndTime; From 9cf481a652d1b0066387c09b099357756552e4de Mon Sep 17 00:00:00 2001 From: Alex Wilde Date: Thu, 16 Jul 2015 20:49:33 +0200 Subject: [PATCH 3/3] Destroy current player before creating a new player. --- src/angular-youtube-embed.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/angular-youtube-embed.js b/src/angular-youtube-embed.js index 18b2b58..a55e515 100644 --- a/src/angular-youtube-embed.js +++ b/src/angular-youtube-embed.js @@ -195,13 +195,13 @@ angular.module('youtube-embed', ['ng']) function loadPlayer () { // If present, use an existing player - if(scope.player && !isEmpty(scope.player) && scope.player.getIframe()) { + if(scope.player && !isEmpty(scope.player) && scope.player.getIframe() && scope.player.cueVideoById === 'function') { var playerVars = ( playerVars ? playerVars : angular.copy(scope.playerVars) ); playerVars.start = playerVars.start || scope.urlStartTime; playerVars.end = playerVars.end || scope.urlEndTime; - + // Use cueVideoById() instead of loadVideoById() so the video doesn't - // start to play automatically. + // start to play automatically. if (scope.videoId) { scope.player.cueVideoById({ videoId:scope.videoId, @@ -219,9 +219,11 @@ angular.module('youtube-embed', ['ng']) } // Otherwise, create a new player else if(scope.videoId || scope.playerVars.list) { + if (scope.player && typeof scope.player.destroy === 'function') { + scope.player.destroy(); + } scope.player = createPlayer(); } - }; var stopWatchingReady = scope.$watch(