@tidal-music/player-web-components 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,270 @@
1
+ import { Y as I, O as g, V as p } from "./basePlayer-a-avZASH-Ux-6s9Ex.js";
2
+ import { N as P, A as y, O as E, k as h, y as S, b as L, R as k } from "./index-tM9JvbA8.js";
3
+ class f extends I {
4
+ #t;
5
+ #i;
6
+ #n;
7
+ #a = !0;
8
+ #o;
9
+ #e;
10
+ #s = !1;
11
+ name = "browserPlayer";
12
+ constructor() {
13
+ super(), this.playbackState = "IDLE";
14
+ const e = () => {
15
+ this.mediaElement && !this.mediaElement.paused && (this.playbackState = "PLAYING");
16
+ }, i = (a) => {
17
+ this.currentStreamingSessionId && a.target instanceof HTMLMediaElement && h.overwriteDuration(
18
+ this.currentStreamingSessionId,
19
+ a.target.duration
20
+ );
21
+ }, t = () => {
22
+ this.playbackState = "STALLED";
23
+ }, s = () => {
24
+ (async () => {
25
+ const a = this.#t;
26
+ if (a) {
27
+ if (this.preloadedStreamingSessionId && a.currentTime === a.duration && (await k(1e3), a.currentTime !== a.duration))
28
+ return;
29
+ this.playbackState = "NOT_PLAYING";
30
+ }
31
+ })().catch(console.error);
32
+ }, n = (a) => {
33
+ if (a instanceof Event) {
34
+ const c = a.target;
35
+ c.readyState > HTMLMediaElement.HAVE_NOTHING && (this.currentTime = c.currentTime);
36
+ }
37
+ }, d = (a) => {
38
+ n(a), this.finishCurrentMediaProduct("completed");
39
+ }, r = (a) => console.error("HTMLMediaElement errored", a), l = () => {
40
+ this.mediaElement && (this.currentTime = this.mediaElement.currentTime, this.seekStart());
41
+ }, o = () => {
42
+ this.mediaElement && (this.currentTime = this.mediaElement.currentTime, this.seekEnd());
43
+ };
44
+ this.#e = {
45
+ durationChangeHandler: i,
46
+ endedHandler: d,
47
+ errorHandler: r,
48
+ pauseHandler: s,
49
+ playHandler: e,
50
+ playingHandler: e,
51
+ seekedHandler: o,
52
+ seekingHandler: l,
53
+ stalledHandler: t,
54
+ timeUpdateHandler: n,
55
+ waitingHandler: t
56
+ }, P().then().catch(console.error), this.#i = y, this.#n = E, this.currentPlayer = this.#i;
57
+ }
58
+ #r() {
59
+ return [this.#i, this.#n].filter((e) => e !== this.#t).pop();
60
+ }
61
+ #d(e, i) {
62
+ this.debugLog(
63
+ "mediaElementEvents",
64
+ i ? "adding to" : "removing from",
65
+ e
66
+ );
67
+ const t = i ? "addEventListener" : "removeEventListener";
68
+ e[t](
69
+ "durationchange",
70
+ this.#e.durationChangeHandler,
71
+ {
72
+ passive: !0
73
+ }
74
+ ), e[t]("play", this.#e.playHandler, {
75
+ passive: !0
76
+ }), e[t](
77
+ "playing",
78
+ this.#e.playingHandler,
79
+ { passive: !0 }
80
+ ), e[t](
81
+ "timeupdate",
82
+ this.#e.timeUpdateHandler,
83
+ { passive: !0 }
84
+ ), e[t](
85
+ "pause",
86
+ this.#e.pauseHandler,
87
+ { passive: !0 }
88
+ ), e[t](
89
+ "ended",
90
+ this.#e.endedHandler,
91
+ { passive: !0 }
92
+ ), e[t](
93
+ "error",
94
+ this.#e.errorHandler,
95
+ { passive: !0 }
96
+ ), e[t](
97
+ "waiting",
98
+ this.#e.waitingHandler,
99
+ { passive: !0 }
100
+ ), e[t](
101
+ "stalled",
102
+ this.#e.stalledHandler,
103
+ { passive: !0 }
104
+ ), e[t](
105
+ "seeking",
106
+ this.#e.seekingHandler,
107
+ { passive: !0 }
108
+ ), e[t](
109
+ "seeked",
110
+ this.#e.seekedHandler,
111
+ { passive: !0 }
112
+ );
113
+ }
114
+ getPosition() {
115
+ return this.debugLog("getPosition"), this.mediaElement && (this.mediaElement.ended ? this.currentTime = 0 : this.currentTime = this.mediaElement.currentTime), this.currentTime;
116
+ }
117
+ async load(e, i) {
118
+ this.debugLog("load", e), this.currentTime = e.assetPosition, this.startAssetPosition = e.assetPosition, await P(), await this.reset(), this.#a = !1, i === "explicit" && (this.playbackState = "NOT_PLAYING");
119
+ const { assetPosition: t, mediaProduct: s, playbackInfo: n, streamInfo: d } = e;
120
+ this.currentStreamingSessionId = d.streamingSessionId;
121
+ const { currentPlayer: r } = this;
122
+ if (!r)
123
+ return;
124
+ const l = new Promise(
125
+ (u) => r.addEventListener("canplay", () => u(), {
126
+ once: !0
127
+ })
128
+ );
129
+ r.src = d.streamUrl, r.currentTime = t, r.load();
130
+ const o = new Promise(
131
+ (u) => r.addEventListener(
132
+ "durationchange",
133
+ (m) => {
134
+ m.target instanceof HTMLMediaElement && u(m.target.duration);
135
+ },
136
+ { once: !0 }
137
+ )
138
+ );
139
+ if (await l, this.currentStreamingSessionId !== d.streamingSessionId)
140
+ return;
141
+ const a = await o, c = g({
142
+ assetPosition: t,
143
+ duration: a,
144
+ playbackInfo: n,
145
+ streamInfo: d
146
+ });
147
+ return h.saveMediaProductTransition(
148
+ d.streamingSessionId,
149
+ { mediaProduct: s, playbackContext: c }
150
+ ), this.debugLog("dispatching mediaProductTransition"), S.dispatchEvent(
151
+ p(s, c)
152
+ ), this.#s ? (this.#s = !1, this.play()) : Promise.resolve();
153
+ }
154
+ async next(e) {
155
+ this.debugLog("next", e), this.playbackState === "IDLE" && (this.playbackState = "NOT_PLAYING");
156
+ const { mediaProduct: i, playbackInfo: t, streamInfo: s } = e, n = this.#r();
157
+ if (this.preloadedStreamingSessionId = s.streamingSessionId, !n)
158
+ return;
159
+ n.src = s.streamUrl, n.load();
160
+ const d = await new Promise(
161
+ (l) => n.addEventListener(
162
+ "durationchange",
163
+ (o) => {
164
+ o.target instanceof HTMLMediaElement && l(o.target.duration);
165
+ },
166
+ { once: !0 }
167
+ )
168
+ ), r = g({
169
+ assetPosition: 0,
170
+ duration: d,
171
+ playbackInfo: t,
172
+ streamInfo: s
173
+ });
174
+ h.saveMediaProductTransition(
175
+ s.streamingSessionId,
176
+ {
177
+ mediaProduct: i,
178
+ playbackContext: r
179
+ }
180
+ );
181
+ }
182
+ pause() {
183
+ this.debugLog("pause"), this.mediaElement && this.mediaElement.pause();
184
+ }
185
+ async play() {
186
+ if (this.debugLog("play"), await this.maybeHardReload(), this.playbackState === "IDLE")
187
+ return this.debugLog("is IDLE, returning early"), this.#s = !0, Promise.resolve();
188
+ "setSinkId" in y && await this.updateOutputDevice(), this.currentStreamingSessionId && this.mediaProductStarted(this.currentStreamingSessionId), this.setStateToXIfNotYInZMs(1e3, "PLAYING", "STALLED"), this.currentPlayer && await this.currentPlayer.play().catch(console.error);
189
+ }
190
+ async playbackEngineEndedHandler(e) {
191
+ if (this.isActivePlayer) {
192
+ const { reason: i } = e.detail;
193
+ i === "completed" && (this.hasNextItem() ? (await this.skipToPreloadedMediaProduct(), await this.play()) : (console.warn("No item preloaded, not progressing."), this.playbackState = "NOT_PLAYING"));
194
+ }
195
+ }
196
+ async reset({ keepPreload: e } = { keepPreload: !1 }) {
197
+ if (this.#a)
198
+ return Promise.resolve();
199
+ this.debugLog("reset"), this.playbackState !== "IDLE" && this.hasStarted() && this.finishCurrentMediaProduct("skip"), this.playbackState = "IDLE", this.detachPlaybackEngineEndedHandler(), this.currentStreamingSessionId = void 0, e || (this.preloadedStreamingSessionId = void 0);
200
+ const { currentPlayer: i } = this;
201
+ return i && i.readyState !== 0 && (i.load(), await new Promise(
202
+ (t) => i.addEventListener("emptied", () => t(), { passive: !0 })
203
+ )), this.#a = !0, Promise.resolve();
204
+ }
205
+ seek(e) {
206
+ this.debugLog("seek", e);
207
+ const { currentPlayer: i } = this, t = e;
208
+ if (i)
209
+ return "fastSeek" in i ? i.fastSeek(t) : i.currentTime = t, new Promise((s) => {
210
+ i.addEventListener("seeked", () => s(i.currentTime), {
211
+ once: !0
212
+ });
213
+ });
214
+ }
215
+ // eslint-disable-next-line @typescript-eslint/require-await
216
+ async skipToPreloadedMediaProduct() {
217
+ const e = h.getMediaProductTransition(
218
+ this.preloadedStreamingSessionId
219
+ );
220
+ if (!e) {
221
+ this.playbackState = "NOT_PLAYING";
222
+ return;
223
+ }
224
+ this.debugLog(
225
+ "skipToPreloadedMediaProduct",
226
+ this.preloadedStreamingSessionId
227
+ );
228
+ const i = this.#r();
229
+ if (i) {
230
+ this.currentPlayer = i, this.currentStreamingSessionId = String(this.preloadedStreamingSessionId), this.preloadedStreamingSessionId = void 0;
231
+ const { mediaProduct: t, playbackContext: s } = e;
232
+ S.dispatchEvent(
233
+ p(t, s)
234
+ ), this.playbackState === "IDLE" && (this.playbackState = "NOT_PLAYING");
235
+ }
236
+ }
237
+ togglePlayback() {
238
+ this.debugLog("togglePlayback"), this.mediaElement && (this.mediaElement.paused ? this.mediaElement.play().catch(console.error) : this.mediaElement.pause());
239
+ }
240
+ // eslint-disable-next-line @typescript-eslint/require-await
241
+ async unloadPreloadedMediaProduct() {
242
+ this.debugLog(
243
+ "unloadPreloadedMediaProduct",
244
+ this.preloadedStreamingSessionId
245
+ ), this.cleanUpStoredPreloadInfo();
246
+ const e = this.#r();
247
+ e && (e.src = "", e.load());
248
+ }
249
+ get currentPlayer() {
250
+ return this.#t;
251
+ }
252
+ set currentPlayer(e) {
253
+ this.debugLog("set currentPlayer", e), this.#t && (this.#d(this.#t, !1), this.#t.load()), e && (this.#t = e, this.#d(e, !0));
254
+ }
255
+ get mediaElement() {
256
+ return this.currentPlayer ?? null;
257
+ }
258
+ get ready() {
259
+ return this.#o;
260
+ }
261
+ get volume() {
262
+ return L("desiredVolumeLevel");
263
+ }
264
+ set volume(e) {
265
+ this.debugLog("Setting volume to", e), this.currentPlayer && (this.currentPlayer.volume = e);
266
+ }
267
+ }
268
+ export {
269
+ f as default
270
+ };