Handle text track change event

This commit is contained in:
2025-10-20 02:15:44 +02:00
parent aa2921c8b4
commit 4f9204f27c

View File

@@ -80,6 +80,9 @@ export class WebEventEmiter implements PlayerEvents {
this._onError = this._onError.bind(this);
this.player.on("error", this._onError);
this._onTextTrackChange = this._onTextTrackChange.bind(this);
this.player.textTracks().on("change", this._onTextTrackChange);
this._onAudioTrackChange = this._onAudioTrackChange.bind(this);
this.player.audioTracks().on("change", this._onAudioTrackChange);
@@ -114,6 +117,8 @@ export class WebEventEmiter implements PlayerEvents {
this.player.off("error", this._onError);
this.player.textTracks().off("change", this._onTextTrackChange);
this.player.audioTracks().off("change", this._onAudioTrackChange);
this.player.videoTracks().off("change", this._onVideoTrackChange);
@@ -238,6 +243,21 @@ export class WebEventEmiter implements PlayerEvents {
this.onError(new VideoError(codeMap[err.code]!, err.message));
}
_onTextTrackChange() {
// @ts-expect-error they define length & index properties via prototype
const tracks: VideoJsTextTracks = this.player.textTracks();
const selected = [...Array(tracks.length)]
.map((_, i) => ({
id: tracks[i]!.id,
label: tracks[i]!.label,
language: tracks[i]!.language,
selected: tracks[i]!.mode === "showing",
}))
.find((x) => x.selected);
this.onTrackChange(selected ?? null);
}
_onAudioTrackChange() {
// @ts-expect-error they define length & index properties via prototype
const tracks: VideoJsTracks = this.player.audioTracks();