Files
notebrook-notes/frontend/src/ui/audio.ts

67 lines
1.8 KiB
TypeScript
Raw Normal View History

2024-09-03 14:50:33 +02:00
import { UINode } from "./node";
export class Audio extends UINode {
private audioElement: HTMLAudioElement;
public constructor(title: string, src: string | MediaStream = "") {
super(title);
this.audioElement = document.createElement("audio");
if (typeof src === "string") {
this.audioElement.src = src; // Set src if it's a string URL
} else if (src instanceof MediaStream) {
this.audioElement.srcObject = src; // Set srcObject if it's a MediaStream
}
this.audioElement.setAttribute("aria-label", title);
this.element.appendChild(this.audioElement);
this.setRole("audio");
}
public getElement(): HTMLElement {
return this.audioElement;
}
public setSource(src: string | MediaStream) {
if (typeof src === "string") {
this.audioElement.src = src;
} else if (src instanceof MediaStream) {
this.audioElement.srcObject = src;
}
return this;
}
public play() {
this.audioElement.play();
return this;
}
public pause() {
this.audioElement.pause();
return this;
}
public setControls(show: boolean) {
this.audioElement.controls = show;
return this;
}
public setLoop(loop: boolean) {
this.audioElement.loop = loop;
return this;
}
public setMuted(muted: boolean) {
this.audioElement.muted = muted;
return this;
}
public setAutoplay(autoplay: boolean) {
this.audioElement.autoplay = autoplay;
return this;
}
public setVolume(volume: number) {
this.audioElement.volume = volume;
return this;
}
}