Make media player seekable

This commit is contained in:
2022-07-26 20:32:58 +00:00
parent 0ecfe402ea
commit 18eec919a0
4 changed files with 57 additions and 2 deletions

View File

@@ -68,7 +68,7 @@ import { BrowserIDMixin } from "./browserID";
- Quickbar tweaks (ctrl+enter)?
x Card-mod preload
- Video player?
- Media_seek
x Media_seek
- Screensavers
x IMPORTANT: FIX DEFAULT HIDING OF ENTITIES
- NOFIX. Home Assistant bug

View File

@@ -2,6 +2,7 @@ export const MediaPlayerMixin = (SuperClass) => {
return class MediaPlayerMixinClass extends SuperClass {
public player;
private _player_enabled;
private _player_update_cooldown;
constructor() {
super();
@@ -12,6 +13,9 @@ export const MediaPlayerMixin = (SuperClass) => {
for (const ev of ["play", "pause", "ended", "volumechange"]) {
this.player.addEventListener(ev, () => this._player_update());
}
for (const ev of ["timeupdate"]) {
this.player.addEventListener(ev, () => this._player_update_choked());
}
this.firstInteraction.then(() => {
this._player_enabled = true;
@@ -39,10 +43,23 @@ export const MediaPlayerMixin = (SuperClass) => {
this.player.muted = Boolean(ev.detail.mute);
else this.player.muted = !this.player.muted;
});
this.addEventListener("command-player-seek", (ev) => {
this.player.currentTime = ev.detail.position;
setTimeout(() => this._player_update(), 10);
});
this.connectionPromise.then(() => this._player_update());
}
private _player_update_choked() {
if (this._player_update_cooldown) return;
this._player_update_cooldown = window.setTimeout(
() => (this._player_update_cooldown = undefined),
3000
);
this._player_update();
}
private _player_update() {
const state = this._player_enabled
? this.player.src
@@ -59,6 +76,8 @@ export const MediaPlayerMixin = (SuperClass) => {
muted: this.player.muted,
src: this.player.src,
state,
media_duration: this.player.duration,
media_position: this.player.currentTime,
},
});
}