From 7f4dfc43e6146fa81f9edffbb22c7c39e1392437 Mon Sep 17 00:00:00 2001 From: Dermot Duffy Date: Sun, 8 Aug 2021 21:52:52 -0700 Subject: [PATCH] Stop video content when the view is changed. --- src/frigate-card.ts | 70 +++++++++++++++++++++++++++++++++++++++------ src/types.ts | 6 ++++ 2 files changed, 68 insertions(+), 8 deletions(-) diff --git a/src/frigate-card.ts b/src/frigate-card.ts index 97ebab96..f72bc70a 100644 --- a/src/frigate-card.ts +++ b/src/frigate-card.ts @@ -5,8 +5,6 @@ // TODO comments per method. -// TODO: Stop the live view video when I hide it. - // TODO Check for HA state presence and validity before using it, otherwise warn. // TODO Add material tooltips @@ -38,7 +36,7 @@ import './editor'; import style from './frigate-card.scss' -import type { FrigateCardConfig, FrigateEvent, GetEventsParameters } from './types'; +import type { FrigateCardConfig, FrigateEvent, GetEventsParameters, ControlVideosParameters } from './types'; import { actionHandler } from './action-handler-directive'; import { CARD_VERSION } from './const'; import { localize } from './localize/localize'; @@ -120,7 +118,7 @@ export class FrigateCard extends LitElement { if (cardConfig.camera_entity.includes(".")) { cardConfig.frigate_camera_name = cardConfig.camera_entity.split('.', 2)[1] } else { - throw new Error(localize('common.invalid_configuration_missing') + ": camera"); + throw new Error(localize('common.invalid_configuration_missing') + ": camera_entity"); } } @@ -271,23 +269,78 @@ export class FrigateCard extends LitElement { ` } + protected _controlVideos({ + stop, + control_live = false, + control_clip = false}: ControlVideosParameters): void { + const controlVideo = (stop: boolean, is_live: boolean, video: HTMLVideoElement) => { + if (video) { + if (stop) { + video.pause(); + video.currentTime = 0; + } else { + if (is_live) { + // If it's a live view, 'fast-forward' to most recent content. + const duration = video.duration; + video.currentTime = duration; + } + video.play(); + } + } + } + if (!this.shadowRoot) { + return; + } + if (control_clip) { + controlVideo( + stop, + false, + this.shadowRoot?.querySelector( + `#frigate-card-clip-player`) as HTMLVideoElement); + } + if (control_live) { + // Don't have direct access to the live video player as it is buried in + // multiple components/shadow-roots, so need to navigate the path to get to