From 3707917903047bcaa07e101952a781098b3b8633 Mon Sep 17 00:00:00 2001 From: Dermot Duffy Date: Mon, 10 Oct 2022 20:00:51 -0700 Subject: [PATCH] Hide video controls briefly on media load. --- src/components/live/live-webrtc.ts | 2 ++ src/patches/ha-hls-player.ts | 21 ++++++++------------- src/utils/media.ts | 29 +++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 13 deletions(-) create mode 100644 src/utils/media.ts diff --git a/src/components/live/live-webrtc.ts b/src/components/live/live-webrtc.ts index 04082148..bf442f16 100644 --- a/src/components/live/live-webrtc.ts +++ b/src/components/live/live-webrtc.ts @@ -14,6 +14,7 @@ import { contentsChanged } from '../../utils/basic.js'; import { dispatchMediaLoadedEvent } from '../../utils/media-info.js'; import { dispatchErrorMessageEvent, renderProgressIndicator } from '../message.js'; import { renderTask } from '../../utils/task.js'; +import { hideMediaControlsTemporarily, MEDIA_LOAD_CONTROLS_HIDE_SECONDS } from '../../utils/media.js'; // Create a wrapper for AlexxIT's WebRTC card // - https://github.com/AlexxIT/WebRTC @@ -181,6 +182,7 @@ export class FrigateCardLiveWebRTCCard extends LitElement { if (onloadeddata) { onloadeddata.call(video, e); } + hideMediaControlsTemporarily(video, MEDIA_LOAD_CONTROLS_HIDE_SECONDS); dispatchMediaLoadedEvent(this, video); }; } diff --git a/src/patches/ha-hls-player.ts b/src/patches/ha-hls-player.ts index 3f0152d6..71bda287 100644 --- a/src/patches/ha-hls-player.ts +++ b/src/patches/ha-hls-player.ts @@ -15,6 +15,10 @@ import { query } from 'lit/decorators/query.js'; import { dispatchErrorMessageEvent } from '../components/message.js'; import { dispatchMediaLoadedEvent } from '../utils/media-info.js'; import liveHAComponentsStyle from '../scss/live-ha-components.scss'; +import { + hideMediaControlsTemporarily, + MEDIA_LOAD_CONTROLS_HIDE_SECONDS, +} from '../utils/media.js'; customElements.whenDefined('ha-hls-player').then(() => { @customElement('frigate-card-ha-hls-player') @@ -67,20 +71,8 @@ customElements.whenDefined('ha-hls-player').then(() => { */ public seek(seconds: number): void { if (this._video) { - // Hide the controls while programatically seeking, and make them - // visible again a short time after the last seek (controls are annoying - // during timeline seeking) - this._video.controls = false; - + hideMediaControlsTemporarily(this._video); this._video.currentTime = seconds; - - if (this._controlsVisibilityTimerID !== null) { - window.clearTimeout(this._controlsVisibilityTimerID); - } - this._controlsVisibilityTimerID = window.setTimeout(() => { - this._video.controls = true; - this._controlsVisibilityTimerID = null; - }, 1000); } } @@ -100,6 +92,9 @@ customElements.whenDefined('ha-hls-player').then(() => { .muted=${this.muted} ?playsinline=${this.playsInline} ?controls=${this.controls} + @loadedmetadata=${() => { + hideMediaControlsTemporarily(this._video, MEDIA_LOAD_CONTROLS_HIDE_SECONDS); + }} @loadeddata=${(e) => { dispatchMediaLoadedEvent(this, e); }} diff --git a/src/utils/media.ts b/src/utils/media.ts new file mode 100644 index 00000000..0e300087 --- /dev/null +++ b/src/utils/media.ts @@ -0,0 +1,29 @@ +// The number of seconds to hide the video controls for after loading (in order +// to give a cleaner UI appearance, see: +// https://github.com/dermotduffy/frigate-hass-card/issues/856 +export const MEDIA_LOAD_CONTROLS_HIDE_SECONDS = 2; +export const MEDIA_SEEK_CONTROLS_HIDE_SECONDS = 1; + +/** + * Temporarily hide media controls. + * @param element Any HTMLElement that has a controls property (e.g. + * HTMLVideoElement, FrigateCardHaHlsPlayer) + * @param seconds The number of seconds to hide the controls for. + */ +export const hideMediaControlsTemporarily = ( + element: HTMLElement & { + controls: boolean; + _controlsHideTimeoutID?: number; + }, + seconds = MEDIA_SEEK_CONTROLS_HIDE_SECONDS, +): void => { + element.controls = false; + + if (element._controlsHideTimeoutID) { + window.clearTimeout(element._controlsHideTimeoutID); + } + element._controlsHideTimeoutID = window.setTimeout(() => { + element.controls = true; + delete element._controlsHideTimeoutID; + }, seconds * 1000); +};