Skip to content

Commit 4cbd302

Browse files
committed
Use an existing player if present when video changes.
See original issue brandly#46 Fix adapted from brandly#48
1 parent b007698 commit 4cbd302

1 file changed

Lines changed: 25 additions & 4 deletions

File tree

src/angular-youtube-embed.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,13 +186,34 @@ angular.module('youtube-embed', ['ng'])
186186
}
187187

188188
function loadPlayer () {
189-
if (scope.videoId || scope.playerVars.list) {
190-
if (scope.player && typeof scope.player.destroy === 'function') {
191-
scope.player.destroy();
189+
// If present, use an existing player
190+
if(scope.player && scope.player.getIframe()) {
191+
var playerVars = ( playerVars ? playerVars : angular.copy(scope.playerVars) );
192+
playerVars.start = playerVars.start || scope.urlStartTime;
193+
playerVars.end = playerVars.end || scope.urlEndTime;
194+
195+
// Use cueVideoById() instead of loadVideoById() so the video doesn't
196+
// start to play automatically.
197+
if (scope.videoId) {
198+
scope.player.cueVideoById({
199+
videoId:scope.videoId,
200+
startSeconds:playerVars.start,
201+
endSeconds:playerVars.end
202+
});
192203
}
193-
204+
else if(scope.playerVars.list) {
205+
scope.player.cuePlaylist({
206+
playlist:scope.playerVars.list,
207+
startSeconds:playerVars.start,
208+
endSeconds:playerVars.end
209+
});
210+
}
211+
}
212+
// Otherwise, create a new player
213+
else if(scope.videoId || scope.playerVars.list) {
194214
scope.player = createPlayer();
195215
}
216+
196217
};
197218

198219
var stopWatchingReady = scope.$watch(

0 commit comments

Comments
 (0)