forked from NamPNQ/bower-videogular-youtube
-
Notifications
You must be signed in to change notification settings - Fork 1
/
youtube.js
191 lines (174 loc) · 9.12 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
"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;
});
};
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", "VG_STATES",
function($rootScope, $window, $timeout, $interval, VG_STATES) {
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,
'autoplay': 0, //Switch autoplay to 1 to autoplay videos
'start': 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.cueVideoById({
videoId: getYoutubeId(url)
});
} else {
$rootScope.$watch('youtubeApiReady', function(value) {
if (value) {
ytplayer = new YT.Player(API.mediaElement[0], {
videoId: getYoutubeId(url),
playerVars: playerVars,
events: {
'onReady': onVideoReady,
'onStateChange': onVideoStateChange
}
});
}
});
}
}
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%'});
$rootScope.$emit('ytVideoReady');
}
function onVideoStateChange(event) {
var player = event.target;
let propagateEvent = event.data;
switch (event.data) {
case YT.PlayerState.ENDED:
API.onComplete();
break;
case YT.PlayerState.PLAYING:
// Trigger onStartPlaying event
var event = new CustomEvent("playing");
API.mediaElement[0].dispatchEvent(event);
API.setState(VG_STATES.PLAY);
break;
case YT.PlayerState.PAUSED:
// NB Videogular calls pause() on the YouTube player to actually stop a video.
// Avoid jumping from the desired "stop" status to "pause" status:
if (API.currentState == VG_STATES.PLAY) {
API.setState(VG_STATES.PAUSE);
}
break;
case YT.PlayerState.BUFFERING:
// Trigger onStartBuffering event
var event = new CustomEvent("waiting");
API.mediaElement[0].dispatchEvent(event);
break;
case YT.PlayerState.CUED:
//No appropriate state
break;
}
$rootScope.$emit('ytVideoStateChange', propagateEvent);
}
function isYoutube(url) {
if (url) {
return url.match(youtubeReg);
}
return false;
}
function onSourceChange(url) {
if (isYoutube(url)) {
initYoutubePlayer(url);
} else {
if (ytplayer) {
destroyYoutubePlayer();
}
}
}
scope.$watch(
function() {
return API.sources;
},
function(newVal, oldVal) {
if (newVal && newVal.length > 0 && newVal[0].src) {
onSourceChange(newVal[0].src.toString());
}
else {
onSourceChange(null);
}
}
);
scope.$on('$destroy', function() {
clearInterval(updateTimer);
});
}
};
}
]);