-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
154 lines (140 loc) · 3.8 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
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
enum ActionHandlers {
PLAY = "play",
PAUSE = "pause",
PREVIOUS_TRACK = "previoustrack",
NEXT_TRACK = "nexttrack",
SEEK_BACKWARD = "seekbackward",
SEEK_FORWARD = "seekforward",
SEEKTO = "seekto",
STOP = "stop",
}
enum PlaybackStates {
PLAYING = "playing",
PAUSED = "paused",
}
export interface Artwork {
src: string;
sizes: string;
type: "image/jpeg" | "image/png";
}
export interface MediaSessionMetadata {
title: string;
siteName: string;
pageName?: string;
artwork: Artwork[];
}
export class MediaSessionManager {
private videoElement: HTMLVideoElement;
private metadata: MediaSessionMetadata;
private isLive = false;
constructor(
videoElement: HTMLVideoElement,
metadata: MediaSessionMetadata,
isLive?: boolean
) {
if (!(videoElement instanceof HTMLVideoElement)) {
throw new Error(
"[MediaSessionManager] videoElement must be an HTMLVideoElement"
);
}
if (
!metadata ||
!metadata.title ||
!metadata.siteName ||
!metadata.artwork
) {
throw new Error("[MediaSessionManager] metadata must be provided");
}
this.videoElement = videoElement;
this.metadata = metadata;
this.isLive = isLive;
this.setup();
}
private setup(): void {
if ("mediaSession" in navigator) {
this.setMetadata();
this.setupEventListeners();
this.setupStateReporter();
}
}
private setMetadata(): void {
navigator.mediaSession.metadata = new MediaMetadata({
title: this.metadata.title,
artist: this.metadata.siteName,
album: this.metadata.pageName || this.metadata.siteName,
artwork: this.metadata.artwork,
});
}
private setupEventListeners(): void {
// Wrap all setActionHandler in try/catch, see: https://github.com/w3c/mediasession/issues/228?ts=2
try {
navigator.mediaSession.setActionHandler(ActionHandlers.PLAY, async () => {
await this.videoElement.play();
navigator.mediaSession.playbackState = PlaybackStates.PLAYING;
});
} catch (e) {
// no-op
}
try {
navigator.mediaSession.setActionHandler(ActionHandlers.PAUSE, () => {
this.videoElement.pause();
navigator.mediaSession.playbackState = PlaybackStates.PAUSED;
});
} catch (e) {
// no-op
}
try {
navigator.mediaSession.setActionHandler(
ActionHandlers.SEEK_BACKWARD,
(details: MediaSessionActionDetails) => {
this.videoElement.currentTime -= details.seekOffset || 15;
this.updatePlaybackState();
}
);
} catch (e) {
// no-op
}
try {
navigator.mediaSession.setActionHandler(
ActionHandlers.SEEK_FORWARD,
(details: MediaSessionActionDetails) => {
this.videoElement.currentTime += details.seekOffset || 15;
this.updatePlaybackState();
}
);
} catch (e) {
// no-op
}
try {
navigator.mediaSession.setActionHandler(
ActionHandlers.SEEKTO,
(details: MediaSessionActionDetails) => {
this.videoElement.currentTime = details.seekTime;
this.updatePlaybackState();
}
);
} catch (e) {
// no-op
}
}
private setupStateReporter(): void {
this.videoElement.addEventListener("timeupdate", () => {
if (
!this.isLive &&
this.videoElement.duration > 0 &&
this.videoElement.currentTime &&
this.videoElement.currentTime <= this.videoElement.duration
) {
this.updatePlaybackState();
}
});
}
private updatePlaybackState(): void {
if (!navigator.mediaSession || !navigator.mediaSession.setPositionState)
return;
navigator.mediaSession.setPositionState({
duration: this.videoElement.duration,
position: this.videoElement.currentTime,
});
}
}