feat: added robust and more decoupled ways on init
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* main player
|
||||
* main player and orchest
|
||||
*
|
||||
* author: db123 at db
|
||||
* creation: 21/4/2026
|
||||
@@ -9,7 +9,7 @@ import { NativeAdapter } from "../adapters";
|
||||
import { AdapterAPI, PlayerConfig } from "../types/index";
|
||||
|
||||
export class VP {
|
||||
private video: HTMLVideoElement | null;
|
||||
private video: HTMLVideoElement;
|
||||
private videoSrc?: NodeListOf<HTMLSourceElement>;
|
||||
private config?: PlayerConfig;
|
||||
private adapter: AdapterAPI;
|
||||
@@ -21,17 +21,55 @@ export class VP {
|
||||
constructor(video: string | HTMLVideoElement, config: PlayerConfig) {
|
||||
this.config = config;
|
||||
|
||||
if (video instanceof HTMLVideoElement) {
|
||||
this.video = video;
|
||||
} else {
|
||||
this.video = document.querySelector(video);
|
||||
}
|
||||
if (!this.video) {
|
||||
throw new Error(`Video element not found: ${video}`);
|
||||
this.video = this.videoElement(video);
|
||||
|
||||
this.videoSrc = this.video?.querySelectorAll<HTMLSourceElement>("source");
|
||||
|
||||
this.adapter = this.adapterInit(config.type);
|
||||
|
||||
this.info();
|
||||
}
|
||||
|
||||
this.videoSrc = this.video?.querySelectorAll("source");
|
||||
this.adapter = new NativeAdapter(this.video);
|
||||
private info(): void {
|
||||
console.info(`
|
||||
====================================
|
||||
VP = VideoPlayer by db
|
||||
https://git.db123.ir/db/VideoPlayer
|
||||
====================================
|
||||
`);
|
||||
}
|
||||
|
||||
private videoElement(video: string | HTMLVideoElement): HTMLVideoElement {
|
||||
let videoEl;
|
||||
if (video instanceof HTMLVideoElement) {
|
||||
videoEl = video;
|
||||
} else {
|
||||
videoEl = document.querySelector<HTMLVideoElement>(video);
|
||||
}
|
||||
if (!videoEl) {
|
||||
throw new Error(`Video element not found: ${video}`);
|
||||
}
|
||||
return videoEl;
|
||||
}
|
||||
|
||||
private adapterInit(type: string = "native"): AdapterAPI {
|
||||
switch (type) {
|
||||
case "native": {
|
||||
return new NativeAdapter(this.video);
|
||||
}
|
||||
|
||||
case "hls": {
|
||||
throw new Error(`Adapter not implemented: ${type}`);
|
||||
}
|
||||
|
||||
case "dash": {
|
||||
throw new Error(`Adapter not implemented: ${type}`);
|
||||
}
|
||||
|
||||
default: {
|
||||
throw new Error(`Adapter not found: ${type}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
load(): void {
|
||||
|
||||
Reference in New Issue
Block a user