Skip to content

Commit

Permalink
Use an existing player if present when video changes.
Browse files Browse the repository at this point in the history
See original issue brandly#46
Fix adapted from brandly#48
  • Loading branch information
alexthewilde committed Jul 16, 2015
1 parent b007698 commit 4cbd302
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions src/angular-youtube-embed.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 4cbd302

Please sign in to comment.