-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
75 lines (66 loc) · 1.57 KB
/
index.ts
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
import { CommonOptions, PlayerAnalytics } from "@api.video/player-analytics";
import Hls from "hls.js";
export type HlsJsApiVideoAnalyticsOptions = CommonOptions;
export class HlsJsApiVideoAnalytics {
private isFirstPlay = true;
private hls: Hls;
private videoElement: HTMLMediaElement | null | undefined;
private seekingStart?: number;
private seekingEnd?: number;
private options?: CommonOptions;
private playerAnalytics!: PlayerAnalytics;
constructor(hls: Hls, options?: HlsJsApiVideoAnalyticsOptions) {
this.hls = hls;
this.options = options || {};
if (this.hls.media && (this.hls as any).url) {
this.init();
} else {
hls.on("hlsManifestLoaded", (event, data) => {
this.init();
});
}
}
private init() {
this.videoElement = this.hls.media;
this.isFirstPlay = true;
if (!this.playerAnalytics) {
this.playerAnalytics = new PlayerAnalytics({
...this.options,
mediaUrl: (this.hls as any).url,
});
}
if (this.videoElement) {
this.playerAnalytics.ovbserveMedia(this.videoElement as HTMLVideoElement);
} else {
console.error("No video element found");
}
}
}
const events = [
"abort",
"canplay",
"canplaythrough",
"durationchange",
"emptied",
"encrypted",
"ended",
"error",
"interruptbegin",
"interruptend",
"loadeddata",
"loadedmetadata",
"loadstart",
"mozaudioavailable",
"pause",
"play",
"playing",
"progress",
"ratechange",
"seeked",
"seeking",
"stalled",
"suspend",
"timeupdate",
"volumechange",
"waiting",
];