@aigamo/nostalgic-diva
Version:
React function components for imperatively controlling embedded players (audio, Niconico, SoundCloud and YouTube) using refs.
1,073 lines (1,072 loc) • 28.7 kB
JavaScript
import p, { createContext as _, useContext as L, useRef as O, useCallback as i, useMemo as W, lazy as d, memo as $, Suspense as j } from "react";
import H from "react-dom";
var n = /* @__PURE__ */ ((t) => (t[t.Trace = 0] = "Trace", t[t.Debug = 1] = "Debug", t[t.Information = 2] = "Information", t[t.Warning = 3] = "Warning", t[t.Error = 4] = "Error", t[t.Critical = 5] = "Critical", t[t.None = 6] = "None", t))(n || {});
class F {
title = "nostalgic-diva";
createMessage(e) {
return `[${this.title}] ${e}`;
}
debug(e, ...a) {
console.debug(this.createMessage(e), ...a);
}
error(e, ...a) {
console.error(this.createMessage(e), ...a);
}
warn(e, ...a) {
console.warn(this.createMessage(e), ...a);
}
isEnabled() {
return !0;
}
log(e, a, ...r) {
switch (e) {
case 1:
this.debug(a, ...r);
break;
case 3:
this.warn(a, ...r);
break;
case 4:
this.error(a, ...r);
break;
}
}
}
var c = /* @__PURE__ */ ((t) => (t.Audio = "Audio", t.Dailymotion = "Dailymotion", t.Niconico = "Niconico", t.SoundCloud = "SoundCloud", t.Twitch = "Twitch", t.Vimeo = "Vimeo", t.YouTube = "YouTube", t))(c || {});
function he(t) {
return c[t] !== void 0;
}
class S {
constructor(e, a, r, s, l) {
this.logger = e, this.type = a, this.player = r, this.options = s, this.controllerFactory = l, this.id = S.nextId++;
}
static nextId = 1;
id;
impl;
createMessage(e) {
return `${this.type}#${this.id} ${e}`;
}
debug(e, ...a) {
this.logger.log(
n.Debug,
this.createMessage(e),
...a
);
}
error(e, ...a) {
this.logger.log(
n.Error,
this.createMessage(e),
...a
);
}
async attach(e) {
if (this.debug("attach", e), this.impl) {
this.debug("player is already attached");
return;
}
this.debug("Attaching player..."), this.impl = new this.controllerFactory(
this.logger,
this.player,
this.options
), await this.impl.attach(e), this.debug("player attached");
}
createPlayerNotAttachedError() {
return new Error("player is not attached");
}
createCommandNotSupportedError(e) {
return new Error(`${e} is not supported`);
}
async detach() {
if (this.debug("detach"), this.impl === void 0)
throw this.createPlayerNotAttachedError();
await this.impl.detach(), this.impl = void 0;
}
async loadVideo(e) {
if (this.debug("loadVideo", e), this.impl === void 0)
throw this.createPlayerNotAttachedError();
if (this.impl.loadVideo === void 0)
throw this.createCommandNotSupportedError("loadVideo");
this.debug("Loading video..."), await this.impl.loadVideo(e), this.debug("video loaded", e);
}
play() {
if (this.debug("play"), this.impl === void 0)
throw this.createPlayerNotAttachedError();
if (this.impl.play === void 0)
throw this.createCommandNotSupportedError("play");
return this.impl.play();
}
pause() {
if (this.debug("pause"), this.impl === void 0)
throw this.createPlayerNotAttachedError();
if (this.impl.pause === void 0)
throw this.createCommandNotSupportedError("pause");
return this.impl.pause();
}
setCurrentTime(e) {
if (this.debug("setCurrentTime", e), this.impl === void 0)
throw this.createPlayerNotAttachedError();
if (this.impl.setCurrentTime === void 0)
throw this.createCommandNotSupportedError("setCurrentTime");
return this.impl.setCurrentTime(e);
}
setVolume(e) {
if (this.debug("setVolume", e), this.impl === void 0)
throw this.createPlayerNotAttachedError();
if (this.impl.setVolume === void 0)
throw this.createCommandNotSupportedError("setVolume");
return this.impl.setVolume(e);
}
setMuted(e) {
if (this.debug("setMuted", e), this.impl === void 0)
throw this.createPlayerNotAttachedError();
if (this.impl.setMuted === void 0)
throw this.createCommandNotSupportedError("setMuted");
return this.impl.setMuted(e);
}
setPlaybackRate(e) {
if (this.debug("setPlaybackRate", e), this.impl === void 0)
throw this.createPlayerNotAttachedError();
if (this.impl.setPlaybackRate === void 0)
throw this.createCommandNotSupportedError("setPlaybackRate");
return this.impl.setPlaybackRate(e);
}
getDuration() {
if (this.debug("getDuration"), this.impl === void 0)
throw this.createPlayerNotAttachedError();
if (this.impl.getDuration === void 0)
throw this.createCommandNotSupportedError("getDuration");
return this.impl.getDuration();
}
getCurrentTime() {
if (this.debug("getCurrentTime"), this.impl === void 0)
throw this.createPlayerNotAttachedError();
if (this.impl.getCurrentTime === void 0)
throw this.createCommandNotSupportedError("getCurrentTime");
return this.impl.getCurrentTime();
}
getVolume() {
if (this.debug("getVolume"), this.impl === void 0)
throw this.createPlayerNotAttachedError();
if (this.impl.getVolume === void 0)
throw this.createCommandNotSupportedError("getVolume");
return this.impl.getVolume();
}
getPlaybackRate() {
if (this.debug("getPlaybackRate"), this.impl === void 0)
throw this.createPlayerNotAttachedError();
if (this.impl.getPlaybackRate === void 0)
throw this.createCommandNotSupportedError("getPlaybackRate");
return this.impl.getPlaybackRate();
}
supports(e) {
if (this.impl === void 0)
throw this.createPlayerNotAttachedError();
return this.impl.supports(e);
}
}
class u {
constructor(e) {
this.type = e;
}
}
const G = /\.(m4a|m4b|mp4a|mpga|mp2|mp2a|mp3|m2a|m3a|wav|weba|aac|oga|spx)($|\?)/i, B = /\.(mp4|og[gv]|webm|mov|m4v)(#t=[,\d+]+)?($|\?)/i;
class z extends u {
constructor() {
super(c.Audio);
}
canPlay(e) {
return G.test(e) || B.test(e);
}
extractVideoId(e) {
return e;
}
}
const k = /^(?:(?:https?):)?(?:\/\/)?(?:www\.)?(?:(?:dailymotion\.com(?:\/embed)?\/video)|dai\.ly)\/([a-zA-Z0-9]+)(?:_[\w_-]+)?(?:[\w.#_-]+)?/;
class X extends u {
constructor() {
super(c.Dailymotion);
}
canPlay(e) {
return k.test(e);
}
extractVideoId(e) {
return k.exec(e)?.[1];
}
}
const D = /(?:www\.|)?nicovideo\.jp\/watch\/(\w+)$/;
class K extends u {
constructor() {
super(c.Niconico);
}
canPlay(e) {
return D.test(e);
}
extractVideoId(e) {
return D.exec(e)?.[1];
}
}
const Z = /(?:soundcloud\.com|snd\.sc)\/[^.]+$/;
class q extends u {
constructor() {
super(c.SoundCloud);
}
canPlay(e) {
return Z.test(e);
}
extractVideoId(e) {
return e;
}
}
const I = /(?:www\.|go\.)?twitch\.tv\/videos\/(\d+)($|\?)/;
class J extends u {
constructor() {
super(c.Twitch);
}
canPlay(e) {
return I.test(e);
}
extractVideoId(e) {
return I.exec(e)?.[1];
}
}
const f = /vimeo\.com\/(\d+)$/;
class Q extends u {
constructor() {
super(c.Vimeo);
}
canPlay(e) {
return f.test(e);
}
extractVideoId(e) {
return f.exec(e)?.[1];
}
}
const N = /(?:youtu\.be\/|youtube(?:-nocookie|education)?\.com\/(?:embed\/|v\/|watch\/|watch\?v=|watch\?.+&v=|shorts\/|live\/))((\w|-){11})|youtube\.com\/playlist\?list=|youtube\.com\/user\//;
class ee extends u {
constructor() {
super(c.YouTube);
}
canPlay(e) {
return N.test(e);
}
extractVideoId(e) {
return N.exec(e)?.[1];
}
}
const te = [
new z(),
new X(),
new K(),
new q(),
new J(),
new Q(),
new ee()
];
function ae(t) {
return te.find((e) => e.canPlay(t));
}
class re {
async attach() {
}
async detach() {
}
async loadVideo(e) {
}
async play() {
}
async pause() {
}
async setCurrentTime(e) {
}
async setVolume(e) {
}
async setMuted(e) {
}
async setPlaybackRate(e) {
}
async getDuration() {
}
async getCurrentTime() {
}
async getVolume() {
}
async getPlaybackRate() {
}
supports(e) {
return !1;
}
}
const A = new re(), U = _(
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
void 0
), pe = ({
children: t
}) => {
const e = O(A), a = i(
(o) => {
e.current = o;
},
[]
), r = i(async (o) => {
await e.current.loadVideo(o);
}, []), s = i(async () => {
await e.current.play();
}, []), l = i(async () => {
await e.current.pause();
}, []), y = i(
async (o) => {
await e.current.setCurrentTime(o), await e.current.play();
},
[]
), m = i(async (o) => {
await e.current.setVolume(o);
}, []), g = i(async (o) => {
await e.current.setMuted(o);
}, []), v = i(
async (o) => {
await e.current.setPlaybackRate(o);
},
[]
), b = i(async () => await e.current.getDuration(), []), C = i(async () => await e.current.getCurrentTime(), []), T = i(async () => await e.current.getVolume(), []), P = i(async () => await e.current.getPlaybackRate(), []), V = i(
(o) => e.current.supports(o),
[]
), Y = W(
() => ({
handleControllerChange: a,
loadVideo: r,
play: s,
pause: l,
setCurrentTime: y,
setVolume: m,
setMuted: g,
setPlaybackRate: v,
getDuration: b,
getCurrentTime: C,
getVolume: T,
getPlaybackRate: P,
supports: V
}),
[
a,
r,
s,
l,
y,
m,
g,
v,
b,
C,
T,
P,
V
]
);
return /* @__PURE__ */ p.createElement(U.Provider, { value: Y }, t);
}, se = () => L(U), ie = {
Audio: d(() => import("./AudioPlayer-o8LWcClA.js")),
Dailymotion: d(() => import("./DailymotionPlayer-DetFPu4s.js")),
Niconico: d(() => import("./NiconicoPlayer-DzGP2JQH.js")),
SoundCloud: d(() => import("./SoundCloudPlayer-ACbN5drz.js")),
Twitch: d(() => import("./TwitchPlayer-Bk0XakuU.js")),
Vimeo: d(() => import("./VimeoPlayer-DhcLwyiy.js")),
YouTube: d(() => import("./YouTubePlayer-COrDr2Qf.js"))
}, oe = new F();
function ne(t) {
const e = ae(t);
if (e === void 0)
return;
const { type: a, extractVideoId: r } = e, s = r(t);
if (s !== void 0)
return { type: a, videoId: s };
}
const le = $(
({
src: t,
options: e,
logger: a = oe,
onControllerChange: r
}) => {
const s = se(), l = i(
(b) => (r ?? s?.handleControllerChange)?.(b),
[s?.handleControllerChange, r]
);
a.log(n.Debug, "NostalgicDiva");
const y = ne(t);
if (y === void 0)
return /* @__PURE__ */ p.createElement("div", { style: { width: "100%", height: "100%" } }, /* @__PURE__ */ p.createElement(
"iframe",
{
src: "about:blank",
title: "about:blank",
style: {
width: "100%",
height: "100%",
border: 0
}
}
));
const { type: m, videoId: g } = y, v = ie[m];
return /* @__PURE__ */ p.createElement(j, { fallback: null }, /* @__PURE__ */ p.createElement(
v,
{
logger: a,
type: m,
onControllerChange: l,
videoId: g,
options: e
}
));
}
);
class ce extends HTMLElement {
static observedAttributes = ["src"];
container;
controller = A;
constructor() {
super(), this.container = this.attachShadow({ mode: "closed" });
}
get src() {
return this.getAttribute("src") ?? "";
}
set src(e) {
this.setAttribute("src", e);
}
#t = {
onError: (e) => this.dispatchEvent(new CustomEvent("error", { detail: e })),
onLoaded: (e) => this.dispatchEvent(new CustomEvent("loaded", { detail: e })),
onPlay: () => this.dispatchEvent(new CustomEvent("play")),
onPause: () => this.dispatchEvent(new CustomEvent("pause")),
onEnded: () => this.dispatchEvent(new CustomEvent("ended")),
onTimeUpdate: (e) => this.dispatchEvent(new CustomEvent("timeupdate", { detail: e }))
};
#a = (e) => {
console.debug(
"[@nostalgic-diva/web-components] handleControllerChange"
), this.controller = e;
};
#e() {
H.render(
/* @__PURE__ */ p.createElement(
le,
{
src: this.src,
options: this.#t,
onControllerChange: this.#a
}
),
this.container
);
}
connectedCallback() {
console.debug("[@nostalgic-diva/web-components] connectedCallback"), this.#e();
}
disconnectedCallback() {
console.debug("[@nostalgic-diva/web-components] disconnectedCallback");
}
attributeChangedCallback() {
console.debug(
"[@nostalgic-diva/web-components] attributeChangedCallback"
), this.#e();
}
async loadVideo(e) {
await this.controller.loadVideo(e);
}
async play() {
await this.controller.play();
}
async pause() {
await this.controller.pause();
}
async setCurrentTime(e) {
await this.controller.setCurrentTime(e);
}
async setVolume(e) {
await this.controller.setVolume(e);
}
async setMuted(e) {
await this.controller.setMuted(e);
}
async setPlaybackRate(e) {
await this.controller.setPlaybackRate(e);
}
async getDuration() {
return await this.controller.getDuration();
}
async getCurrentTime() {
return await this.controller.getCurrentTime();
}
async getVolume() {
return await this.controller.getVolume();
}
async getPlaybackRate() {
return await this.controller.getPlaybackRate();
}
supports(e) {
return this.controller.supports(e);
}
}
function ye() {
customElements.define("nostalgic-diva", ce);
}
class h {
constructor(e, a, r) {
this.logger = e, this.player = a, this.options = r, this.logger.log(n.Debug, "ctor");
}
supports(e) {
return this[e] !== void 0;
}
}
class me extends h {
async attach() {
this.player.onerror = (e) => this.options?.onError?.(e), this.player.onloadeddata = () => this.options?.onLoaded?.({ id: this.player.src }), this.player.onplay = () => this.options?.onPlay?.(), this.player.onpause = () => this.options?.onPause?.(), this.player.onended = () => this.options?.onEnded?.(), this.player.ontimeupdate = () => {
this.options?.onTimeUpdate?.({
duration: this.player.duration,
percent: this.player.currentTime / this.player.duration,
seconds: this.player.currentTime
});
};
}
async detach() {
this.player.onerror = null, this.player.onloadeddata = null, this.player.onplay = null, this.player.onpause = null, this.player.onended = null, this.player.ontimeupdate = null;
}
async loadVideo(e) {
this.player.src = e;
}
async play() {
this.player.play();
}
async pause() {
this.player.pause();
}
async setCurrentTime(e) {
this.player.currentTime = e;
}
async setVolume(e) {
this.player.volume = e;
}
async setMuted(e) {
this.player.muted = e;
}
async setPlaybackRate(e) {
this.player.playbackRate = e;
}
async getDuration() {
return this.player.duration;
}
async getCurrentTime() {
return this.player.currentTime;
}
async getVolume() {
return this.player.volume;
}
async getPlaybackRate() {
return this.player.playbackRate;
}
}
const R = [
"apiready",
"seeked",
"video_end",
"durationchange",
"pause",
"playing",
"error"
];
class ge extends h {
handlePlayerEvents = (e) => {
switch (e.type) {
case "apiready":
this.options?.onLoaded?.({ id: this.player.video.videoId });
break;
case "seeked":
this.options?.onTimeUpdate?.({
duration: this.player.duration,
percent: this.player.currentTime / this.player.duration,
seconds: this.player.currentTime
});
break;
case "video_end":
this.options?.onEnded?.();
break;
case "durationchange":
break;
case "pause":
this.options?.onPause?.();
break;
case "playing":
this.options?.onPlay?.();
break;
case "error":
this.options?.onError?.(e);
break;
}
};
async attach(e) {
for (const a of R)
this.player.addEventListener(a, this.handlePlayerEvents);
}
async detach() {
for (const e of R)
this.player.removeEventListener(e, this.handlePlayerEvents);
}
async loadVideo(e) {
this.player.load(e);
}
async play() {
this.player.play();
}
async pause() {
this.player.pause();
}
async setCurrentTime(e) {
this.player.seek(e);
}
async setVolume(e) {
this.player.setVolume(e);
}
async setMuted(e) {
this.player.setMuted(e);
}
setPlaybackRate = void 0;
async getDuration() {
return this.player.duration;
}
async getCurrentTime() {
return this.player.currentTime;
}
async getVolume() {
return this.player.volume;
}
getPlaybackRate = void 0;
}
var w = /* @__PURE__ */ ((t) => (t[t.Play = 2] = "Play", t[t.Pause = 3] = "Pause", t[t.End = 4] = "End", t))(w || {});
class E extends h {
static origin = "https://embed.nicovideo.jp";
duration;
currentTime;
volume;
handleMessage = (e) => {
if (e.origin !== E.origin) return;
const a = e.data;
switch (a.eventName) {
case "playerStatusChange":
this.logger.log(
n.Debug,
`player status changed: ${w[a.data.playerStatus] ?? a.data.playerStatus}`
);
break;
case "statusChange":
switch (this.logger.log(
n.Debug,
`status changed: ${w[a.data.playerStatus] ?? a.data.playerStatus}`
), a.data.playerStatus) {
case 2:
this.options?.onPlay?.();
break;
case 3:
this.options?.onPause?.();
break;
case 4:
this.options?.onEnded?.();
break;
}
break;
case "playerMetadataChange":
a.data.duration !== void 0 && (this.duration = a.data.duration / 1e3), this.currentTime = a.data.currentTime === void 0 ? void 0 : a.data.currentTime / 1e3, this.volume = a.data.volume, this.options?.onTimeUpdate?.({
duration: this.duration,
percent: this.currentTime !== void 0 && this.duration !== void 0 ? this.currentTime / this.duration : void 0,
seconds: this.currentTime
});
break;
case "loadComplete":
this.logger.log(n.Debug, "load completed"), this.duration = a.data.videoInfo.lengthInSeconds, this.options?.onLoaded?.({ id: a.data.videoInfo.watchId });
break;
case "error":
this.options?.onError?.(a);
break;
case "player-error:video:play":
case "player-error:video:seek":
this.options?.onError?.(a);
break;
default:
this.logger.log(
n.Debug,
"message",
a.eventName,
a.data
);
break;
}
};
async attach() {
window.addEventListener("message", this.handleMessage);
}
async detach() {
window.removeEventListener("message", this.handleMessage);
}
async loadVideo(e) {
return new Promise((a, r) => {
this.duration = void 0, this.currentTime = void 0, this.player.onload = () => {
this.player.onload = null, a();
}, this.player.src = `https://embed.nicovideo.jp/watch/${e}?jsapi=1&playerId=1`;
});
}
// https://blog.hayu.io/web/create/nicovideo-embed-player-api/.
postMessage(e) {
this.player.contentWindow?.postMessage(
{
...e,
playerId: "1",
sourceConnectorType: 1
},
E.origin
);
}
async play() {
this.postMessage({ eventName: "play" });
}
async pause() {
this.postMessage({ eventName: "pause" });
}
async setCurrentTime(e) {
this.postMessage({ eventName: "seek", data: { time: e * 1e3 } });
}
async setVolume(e) {
this.postMessage({
eventName: "volumeChange",
data: { volume: e }
});
}
async setMuted(e) {
this.postMessage({
eventName: "mute",
data: { mute: e }
});
}
setPlaybackRate = void 0;
async getDuration() {
return this.duration;
}
async getCurrentTime() {
return this.currentTime;
}
async getVolume() {
return this.volume;
}
getPlaybackRate = void 0;
}
class M extends h {
getDurationCore() {
return new Promise((e, a) => {
this.player.getDuration(e);
});
}
attach(e) {
return new Promise((a, r) => {
this.player.bind(SC.Widget.Events.READY, () => {
this.player.bind(
SC.Widget.Events.PLAY_PROGRESS,
async (s) => {
const l = await this.getDurationCore();
this.options?.onTimeUpdate?.({
duration: l / 1e3,
percent: s.currentPosition / l,
seconds: s.currentPosition / 1e3
});
}
), this.player.bind(
SC.Widget.Events.ERROR,
(s) => this.options?.onError?.(s)
), this.player.bind(
SC.Widget.Events.PLAY,
() => this.options?.onPlay?.()
), this.player.bind(
SC.Widget.Events.PAUSE,
() => this.options?.onPause?.()
), this.player.bind(
SC.Widget.Events.FINISH,
() => this.options?.onEnded?.()
), this.options?.onLoaded?.({ id: e }), a();
});
});
}
async detach() {
this.player.unbind(SC.Widget.Events.READY), this.player.unbind(SC.Widget.Events.PLAY_PROGRESS), this.player.unbind(SC.Widget.Events.ERROR), this.player.unbind(SC.Widget.Events.PLAY), this.player.unbind(SC.Widget.Events.PAUSE), this.player.unbind(SC.Widget.Events.FINISH);
}
static playerLoadAsync(e, a, r) {
return new Promise((s, l) => {
e.load(a, { ...r, callback: s });
});
}
async loadVideo(e) {
await M.playerLoadAsync(this.player, e, {
auto_play: !0
}), this.options?.onLoaded?.({ id: e });
}
async play() {
this.player.play();
}
async pause() {
this.player.pause();
}
async setCurrentTime(e) {
this.player.seekTo(e * 1e3);
}
async setVolume(e) {
this.player.setVolume(e * 100);
}
async setMuted(e) {
this.setVolume(
e ? 0 : 1
/* TODO */
);
}
setPlaybackRate = void 0;
async getDuration() {
return await this.getDurationCore() / 1e3;
}
getCurrentTimeCore() {
return new Promise((e, a) => {
this.player.getPosition(e);
});
}
async getCurrentTime() {
return await this.getCurrentTimeCore() / 1e3;
}
getVolumeCore() {
return new Promise((e, a) => {
this.player.getVolume(e);
});
}
async getVolume() {
return await this.getVolumeCore() / 100;
}
getPlaybackRate = void 0;
}
class ve extends h {
handleReady = () => {
this.options?.onLoaded?.({ id: this.player.getVideo() });
};
handlePlay = () => {
this.options?.onPlay?.();
};
handlePause = () => {
this.options?.onPause?.();
};
handleEnded = () => {
this.options?.onEnded?.();
};
handleSeek = () => {
this.options?.onTimeUpdate?.({
duration: 0,
percent: 0,
seconds: 0
});
};
async attach(e) {
this.player.addEventListener(Twitch.Player.READY, this.handleReady), this.player.addEventListener(Twitch.Player.PLAYING, this.handlePlay), this.player.addEventListener(Twitch.Player.PAUSE, this.handlePause), this.player.addEventListener(Twitch.Player.ENDED, this.handleEnded), this.player.addEventListener(Twitch.Player.SEEK, this.handleSeek);
}
async detach() {
}
async loadVideo(e) {
this.player.setVideo(e, 0);
}
async play() {
this.player.play();
}
async pause() {
this.player.pause();
}
async setCurrentTime(e) {
this.player.seek(e);
}
async setVolume(e) {
this.player.setVolume(e);
}
async setMuted(e) {
this.player.setMuted(e);
}
setPlaybackRate = void 0;
async getDuration() {
return this.player.getDuration();
}
async getCurrentTime() {
return this.player.getCurrentTime();
}
async getVolume() {
return this.player.getVolume();
}
getPlaybackRate = void 0;
}
class be extends h {
async attach() {
await this.player.ready(), this.player.on("error", (e) => this.options?.onError?.(e)), this.player.on(
"loaded",
(e) => this.options?.onLoaded?.({ id: e.id.toString() })
), this.player.on("play", () => this.options?.onPlay?.()), this.player.on("pause", () => this.options?.onPause?.()), this.player.on("ended", () => this.options?.onEnded?.()), this.player.on("timeupdate", (e) => {
this.options?.onTimeUpdate?.({
duration: e.duration,
percent: e.percent,
seconds: e.seconds
});
});
}
async detach() {
this.player.off("error"), this.player.off("loaded"), this.player.off("play"), this.player.off("pause"), this.player.off("ended"), this.player.off("timeupdate");
}
async loadVideo(e) {
await this.player.loadVideo(e);
}
async play() {
await this.player.play();
}
async pause() {
await this.player.pause();
}
async setCurrentTime(e) {
await this.player.setCurrentTime(e);
}
async setVolume(e) {
await this.player.setVolume(e);
}
async setMuted(e) {
await this.player.setMuted(e);
}
async setPlaybackRate(e) {
await this.player.setPlaybackRate(e);
}
async getDuration() {
return this.player.getDuration();
}
async getCurrentTime() {
return this.player.getCurrentTime();
}
async getVolume() {
return this.player.getVolume();
}
async getPlaybackRate() {
return this.player.getPlaybackRate();
}
}
var x = /* @__PURE__ */ ((t) => (t[t.UNSTARTED = -1] = "UNSTARTED", t[t.ENDED = 0] = "ENDED", t[t.PLAYING = 1] = "PLAYING", t[t.PAUSED = 2] = "PAUSED", t[t.BUFFERING = 3] = "BUFFERING", t[t.CUED = 5] = "CUED", t))(x || {});
class we extends h {
previousTime;
timeUpdateIntervalId;
clearTimeUpdateInterval() {
this.logger.log(
n.Debug,
"clearTimeUpdateInterval",
this.timeUpdateIntervalId
), window.clearInterval(this.timeUpdateIntervalId), this.timeUpdateIntervalId = void 0;
}
invokeTimeUpdate(e) {
const a = e.getCurrentTime();
if (a === this.previousTime) return;
const r = e.getDuration();
this.options?.onTimeUpdate?.({
duration: r,
percent: a / r,
seconds: a
}), this.previousTime = a;
}
setTimeUpdateInterval() {
this.logger.log(n.Debug, "setTimeUpdateInterval"), this.clearTimeUpdateInterval(), this.timeUpdateIntervalId = window.setInterval(
() => this.invokeTimeUpdate(this.player),
250
), this.logger.log(
n.Debug,
"timeUpdateIntervalId",
this.timeUpdateIntervalId
), this.invokeTimeUpdate(this.player);
}
attach(e) {
return new Promise((a, r) => {
this.player.addEventListener("onReady", async () => {
this.player.addEventListener(
"onError",
(s) => this.options?.onError?.(s.data)
), this.player.addEventListener(
"onStateChange",
(s) => {
switch (this.logger.log(
n.Debug,
`state changed: ${x[s.data]}`
), s.data) {
case YT.PlayerState.CUED:
this.options?.onLoaded?.({ id: e });
break;
case YT.PlayerState.PLAYING:
this.options?.onPlay?.(), this.setTimeUpdateInterval();
break;
case YT.PlayerState.PAUSED:
this.options?.onPause?.(), this.clearTimeUpdateInterval();
break;
case YT.PlayerState.ENDED:
this.options?.onEnded?.(), this.clearTimeUpdateInterval();
break;
}
}
), await this.loadVideo(e), a();
});
});
}
async detach() {
this.clearTimeUpdateInterval();
}
async loadVideo(e) {
this.previousTime = void 0, this.player.cueVideoById(e);
}
async play() {
this.player.playVideo();
}
async pause() {
this.player.pauseVideo();
}
async setCurrentTime(e) {
this.player.seekTo(e), this.invokeTimeUpdate(this.player);
}
async setVolume(e) {
this.player.setVolume(e * 100);
}
async setMuted(e) {
e ? this.player.mute() : this.player.unMute();
}
async setPlaybackRate(e) {
this.player.setPlaybackRate(e);
}
async getDuration() {
return this.player.getDuration();
}
async getCurrentTime() {
return this.player.getCurrentTime();
}
async getVolume() {
return this.player.getVolume() / 100;
}
async getPlaybackRate() {
return this.player.getPlaybackRate();
}
}
export {
me as A,
ge as D,
n as L,
E as N,
S as P,
M as S,
ve as T,
be as V,
we as Y,
le as a,
pe as b,
ce as c,
ye as d,
c as e,
ae as f,
A as n,
se as u,
he as v
};
//# sourceMappingURL=index-DzXJDFxB.js.map