forked from NamPNQ/bower-videogular-youtube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
youtube.js
141 lines (133 loc) · 6.65 KB
/
youtube.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
"use strict";
angular.module("info.vietnamcode.nampnq.videogular.plugins.youtube", [])
.run(['$rootScope', '$window',
function($rootScope, $window) {
$rootScope.youtubeApiReady = false;
$window.onYouTubeIframeAPIReady = function() {
$rootScope.$apply(function() {
$rootScope.youtubeApiReady = true;
});
};
console.log("Init youtube api");
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
}
])
.directive(
"vgYoutube", ["$rootScope", "$window", "$timeout", "$interval",
function($rootScope, $window, $timeout, $interval) {
return {
restrict: "A",
require: "^videogular",
link: function(scope, elem, attr, API) {
var ytplayer, updateTimer, optionsArr, playerVars;
var youtubeReg = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/;
optionsArr = attr.vgYoutube !== null ? attr.vgYoutube.split(";") : null;
playerVars = {
controls: 0,
showinfo: 0,
rel: 0
};
if (optionsArr !== null) {
optionsArr.forEach(function (item) {
var keyValuePair = item.split("=");
if (playerVars.hasOwnProperty(keyValuePair[0])) {
playerVars[keyValuePair[0]] = keyValuePair[1] || 0;
}
});
}
function getYoutubeId(url) {
return url.match(youtubeReg)[2];
}
function initYoutubePlayer(url) {
if (ytplayer) {
ytplayer.loadVideoById({
videoId: getYoutubeId(url)
});
} else {
$rootScope.$watch('youtubeApiReady', function(value) {
if (value) {
console.log("Api loaded..");
ytplayer = new YT.Player(API.mediaElement[0], {
videoId: getYoutubeId(url),
playerVars: playerVars,
events: {
'onReady': onVideoReady
}
});
}
});
}
}
function destroyYoutubePlayer() {
ytplayer.destroy();
}
function onVideoReady() {
//Define some property, method for player
API.mediaElement[0].__defineGetter__("currentTime", function () {
return ytplayer.getCurrentTime();
});
API.mediaElement[0].__defineSetter__("currentTime", function (seconds) {
return ytplayer.seekTo(seconds, true);
});
API.mediaElement[0].__defineGetter__("duration", function () {
return ytplayer.getDuration();
});
API.mediaElement[0].__defineGetter__("paused", function () {
return ytplayer.getPlayerState() != YT.PlayerState.PLAYING;
});
API.mediaElement[0].__defineGetter__("videoWidth", function () {
return ytplayer.a.width;
});
API.mediaElement[0].__defineGetter__("videoHeight", function () {
return ytplayer.a.height;
});
API.mediaElement[0].__defineGetter__("volume", function () {
return ytplayer.getVolume() / 100.0;
});
API.mediaElement[0].__defineSetter__("volume", function (volume) {
return ytplayer.setVolume(volume * 100.0);
});
API.mediaElement[0].play = function () {
ytplayer.playVideo();
};
API.mediaElement[0].pause = function () {
ytplayer.pauseVideo();
};
function updateTime() {
API.onUpdateTime({
target: API.mediaElement[0]
});
}
updateTimer = setInterval(updateTime, 600);
angular.element(ytplayer.getIframe()).css({'width':'100%','height':'100%'});
}
function isYoutube(url) {
return url.match(youtubeReg);
}
function onSourceChange(url) {
if (isYoutube(url)) {
initYoutubePlayer(url);
} else {
if (ytplayer) {
destroyYoutubePlayer();
}
}
}
scope.$watch(
function() {
return API.sources;
},
function(newVal, oldVal) {
onSourceChange(newVal[0].src);
}
);
scope.$on('$destroy', function() {
clearInterval(updateTimer);
});
}
};
}
]);