Files
VideoPlayer/src/core/VideoPlayer.ts

109 lines
2.2 KiB
TypeScript

/**
* main player and orchest
*
* author: db123 at db
* creation: 21/4/2026
*/
import { adapterRegistry } from '../adapters';
import { AdapterAPI, PlayerConfig } from '../types/index';
import { BaseUI } from '../ui';
export class VP {
private video: HTMLVideoElement;
private videoSrc?: NodeListOf<HTMLSourceElement>;
private config?: PlayerConfig;
private adapter: AdapterAPI;
private ui?: BaseUI;
/**
* @param video The targeted video element or id
* @param config The config array
*/
constructor(video: string | HTMLVideoElement, config: PlayerConfig) {
this.config = config;
this.video = this.videoElement(video);
this.videoSrc = this.video?.querySelectorAll<HTMLSourceElement>('source');
if (!this.config.type) {
throw new Error('Type not specified in config.');
}
this.adapter = adapterRegistry.createAdapter(this.config.type, this.video, {
src: this.config.adapters?.src,
});
if (config.controls) {
this.ui = new BaseUI(this, this.video, this.config.prefixClass);
}
this.info();
}
private info(): void {
console.info(`
====================================
VP = VideoPlayer by db
https://git.db123.ir/db/VideoPlayer
====================================
`);
}
destroy(): void {
this.ui?.destroy();
this.adapter.destroy();
}
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;
}
load(): void {
this.adapter.load();
}
play(): void {
this.adapter.play();
}
pause(): void {
this.adapter.pause();
}
get time(): number {
return this.adapter.time;
}
set time(time: number) {
this.adapter.time = time;
}
get volume(): number {
return this.adapter.volume;
}
set volume(volume: number) {
this.adapter.volume = volume;
}
get muted(): boolean {
return this.adapter.muted;
}
set muted(muted: boolean) {
this.adapter.muted = muted;
}
duration(): number {
return this.adapter.duration();
}
}