Make media player seekable

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

View File

@ -624,6 +624,9 @@ 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;
if (!this.player.ended)
@ -653,8 +656,18 @@ const MediaPlayerMixin = (SuperClass) => {
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());
}
_player_update_choked() {
if (this._player_update_cooldown)
return;
this._player_update_cooldown = window.setTimeout(() => (this._player_update_cooldown = undefined), 3000);
this._player_update();
}
_player_update() {
const state = this._player_enabled
? this.player.src
@ -671,6 +684,8 @@ const MediaPlayerMixin = (SuperClass) => {
muted: this.player.muted,
src: this.player.src,
state,
media_duration: this.player.duration,
media_position: this.player.currentTime,
},
});
}
@ -2166,7 +2181,7 @@ const BrowserIDMixin = (SuperClass) => {
- 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

@ -15,6 +15,7 @@ from homeassistant.components.media_player.const import (
MEDIA_TYPE_MUSIC,
MEDIA_TYPE_URL,
SUPPORT_BROWSE_MEDIA,
SUPPORT_SEEK,
)
from homeassistant.const import (
STATE_UNAVAILABLE,
@ -24,6 +25,8 @@ from homeassistant.const import (
STATE_UNKNOWN,
)
from homeassistant.util import dt
from .entities import BrowserModEntity
from .const import DOMAIN, DATA_ADDERS
@ -72,6 +75,7 @@ class BrowserModPlayer(BrowserModEntity, MediaPlayerEntity):
| SUPPORT_VOLUME_SET
| SUPPORT_VOLUME_MUTE
| SUPPORT_BROWSE_MEDIA
| SUPPORT_SEEK
)
@property
@ -82,6 +86,20 @@ class BrowserModPlayer(BrowserModEntity, MediaPlayerEntity):
def is_volume_muted(self):
return self._data.get("player", {}).get("muted", False)
@property
def media_duration(self):
duration = self._data.get("player", {}).get("media_duration", None)
return float(duration) if duration is not None else None
@property
def media_position(self):
position = self._data.get("player", {}).get("media_position", None)
return float(position) if position is not None else None
@property
def media_position_updated_at(self):
return dt.utcnow()
def set_volume_level(self, volume):
self.browser.send("player-set-volume", volume_level=volume)
@ -115,3 +133,6 @@ class BrowserModPlayer(BrowserModEntity, MediaPlayerEntity):
def media_stop(self):
self.browser.send("player-stop")
def media_seek(self, position):
self.browser.send("player-seek", position=position)

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,
},
});
}