Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

player(ifame) load once even if id,url,list update #143

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dist/angular-youtube-embed.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-youtube-embed",
"version": "1.3.1",
"version": "1.3.2",
"author": "Matthew Brandly",
"license": "MIT",
"main": "index.js",
Expand Down
57 changes: 37 additions & 20 deletions src/angular-youtube-embed.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ angular.module('youtube-embed', [])
scope.playerHeight = scope.playerHeight || 390;
scope.playerWidth = scope.playerWidth || 640;
scope.playerVars = scope.playerVars || {};
scope.playerReady = false

// YT calls callbacks outside of digest cycle
function applyBroadcast () {
Expand All @@ -164,6 +165,7 @@ angular.module('youtube-embed', [])
}

function onPlayerReady (event) {
scope.playerReady = true
applyBroadcast(eventPrefix + 'ready', scope.player, event);
}

Expand All @@ -177,7 +179,6 @@ angular.module('youtube-embed', [])
var player = new YT.Player(playerId, {
height: scope.playerHeight,
width: scope.playerWidth,
videoId: scope.videoId,
playerVars: playerVars,
events: {
onReady: onPlayerReady,
Expand All @@ -190,50 +191,66 @@ angular.module('youtube-embed', [])
return player;
}

function loadPlayer () {
if (scope.videoId || scope.playerVars.list) {
if (scope.player && typeof scope.player.destroy === 'function') {
scope.player.destroy();
}
function isExist (obj) {
if (obj == undefined || obj == null) {
return false
}
else {
return true
}
}

scope.player = createPlayer();
function loadPlayer () {
if (scope.player && typeof scope.player.destroy === 'function') {
scope.player.destroy();
}

scope.player = createPlayer();
};

scope.$watch(function () {
return scope.utils.ready;
}, function (ready) {
if (ready && !isExist(scope.player)) {
loadPlayer()
}
});

var stopWatchingReady = scope.$watch(
function () {
return scope.utils.ready
return scope.playerReady
// Wait until one of them is defined...
&& (typeof scope.videoUrl !== 'undefined'
|| typeof scope.videoId !== 'undefined'
|| typeof scope.playerVars.list !== 'undefined');
&& (isExist(scope.videoUrl)
|| isExist(scope.videoId)
|| isExist(scope.playerVars.list));
},
function (ready) {
if (ready) {
stopWatchingReady();

// URL takes first priority
if (typeof scope.videoUrl !== 'undefined') {
if (isExist(scope.videoUrl)) {
scope.$watch('videoUrl', function (url) {
scope.videoId = scope.utils.getIdFromURL(url);
scope.urlStartTime = scope.utils.getTimeFromURL(url);

loadPlayer();
scope.player.loadVideoByUrl(url);
});

// then, a video ID
} else if (typeof scope.videoId !== 'undefined') {
scope.$watch('videoId', function () {
scope.urlStartTime = null;
loadPlayer();
} else if (isExist(scope.videoId)) {
scope.$watch('videoId', function (id) {
scope.player.loadVideoById(id);
});


// finally, a list
} else {
} else if (isExist(scope.playerVars.list)){
scope.$watch('playerVars.list', function () {
scope.urlStartTime = null;
loadPlayer();
scope.player.loadPlaylist(scope.playerVars.list);
});
} else {
scope.player.stopVideo()
}
}
});
Expand Down