-
Notifications
You must be signed in to change notification settings - Fork 4
/
vg-hls.js
executable file
·77 lines (71 loc) · 2.91 KB
/
vg-hls.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
/**
* @ngdoc directive
* @name com.2fdevs.videogular.plugins.dash.directive:vgHls
* @restrict A
* @description
* Adds HLS support for vg-media.
* This plugin requires hls.js file available at hls.js project:
* https://github.com/dailymotion/hls.js
*
* <pre>
* <videogular vg-theme="config.theme.url" vg-autoplay="config.autoPlay">
* <vg-media vg-src="sources" vg-hls></vg-media>
* </videogular>
* </pre>
*
*/
"use strict";
angular.module("com.2fdevs.videogular.plugins.hls", [])
.directive("vgHls", ['$log', function ($log) {
return {
restrict: "A",
require: "^videogular",
link: function (scope, elem, attr, API) {
var context;
var player;
var hlsTypeRegEx = /^application\/x-mpegURL/i;
//Proceed augmenting behavior only if the browser is capable of playing HLS
if(Hls.isSupported()) {
//Returns true if the source has the standard HLS type defined OR an .m3u8 extension.
scope.isHLS = function isHLS(source) {
var hasHlsType = hlsTypeRegEx.test(source.type);
var hasHlsExtension = source.src.indexOf && (source.src.indexOf(".m3u8") > 0);
return hasHlsType || hasHlsExtension;
};
scope.onSourceChange = function onSourceChange(source) {
var url = source.src;
// It's HLS, we use Hls.js
if (scope.isHLS(source)) {
var video = API.mediaElement[0];
var hls = new Hls();
hls.loadSource(url);
hls.attachMedia(API.mediaElement[0]);
hls.on(Hls.Events.MANIFEST_PARSED,function() {
if(API.autoPlay){
video.play();
}
});
}
else if (player) {
//not HLS, but the Hls.js player is still wired up
//Dettach Hls.js from the mediaElement
player.reset();
player = null;
//player.reset() wipes out the new url already applied, so have to reapply
API.mediaElement.attr('src', url);
API.stop();
}
};
scope.$watch(
function () {
return API.sources;
},
function (newVal/*, oldVal*/) {
scope.onSourceChange(newVal[0]);
}
);
}
}
}
}
]);