Merge pull request #272 from dermotduffy/pause-clips

Pause previous clip when the user moves to the next one
This commit is contained in:
Dermot Duffy
2022-01-20 20:48:35 -08:00
committed by GitHub
3 changed files with 67 additions and 22 deletions
+4 -1
View File
@@ -37,7 +37,10 @@ export class FrigateCardCarousel extends LitElement {
if (!this._carousel) { if (!this._carousel) {
this.updateComplete.then(() => { this.updateComplete.then(() => {
this._initCarousel(); // Re-check for the carousel to prevent a double init.
if (!this._carousel) {
this._initCarousel();
}
}); });
} }
} }
+55 -18
View File
@@ -31,7 +31,11 @@ import {
} from './thumbnail-carousel.js'; } from './thumbnail-carousel.js';
import { ResolvedMediaCache, ResolvedMediaUtil } from '../resolved-media.js'; import { ResolvedMediaCache, ResolvedMediaUtil } from '../resolved-media.js';
import { View } from '../view.js'; import { View } from '../view.js';
import { contentsChanged, createMediaShowInfo, dispatchErrorMessageEvent } from '../common.js'; import {
contentsChanged,
createMediaShowInfo,
dispatchErrorMessageEvent,
} from '../common.js';
import { renderProgressIndicator } from '../components/message.js'; import { renderProgressIndicator } from '../components/message.js';
import './next-prev-control.js'; import './next-prev-control.js';
@@ -235,17 +239,17 @@ export class FrigateCardViewerCarousel extends FrigateCardMediaCarousel {
* @param changedProperties The properties that were changed in this render. * @param changedProperties The properties that were changed in this render.
*/ */
updated(changedProperties: PropertyValues): void { updated(changedProperties: PropertyValues): void {
if (changedProperties.has('viewerConfig')) { if (this._carousel && changedProperties.has('viewerConfig')) {
this._destroyCarousel(); this._destroyCarousel();
} }
if (changedProperties.has('view')) { if (this._carousel && changedProperties.has('view')) {
const oldView = changedProperties.get('view') as View | undefined; const oldView = changedProperties.get('view') as View | undefined;
if (oldView) { if (oldView) {
if (oldView.target != this.view?.target) { if (oldView.target != this.view?.target) {
// If the media target is different entirely, reset the carousel. // If the media target is different entirely, reset the carousel.
this._destroyCarousel(); this._destroyCarousel();
} else if (this._carousel && this.view?.childIndex != oldView.childIndex) { } else if (this.view?.childIndex != oldView.childIndex) {
const slide = this._getSlideForChild(this.view?.childIndex); const slide = this._getSlideForChild(this.view?.childIndex);
if (slide !== undefined && slide !== this.carouselSelected()) { if (slide !== undefined && slide !== this.carouselSelected()) {
// If the media target is the same as already loaded, but isn't of // If the media target is the same as already loaded, but isn't of
@@ -528,23 +532,55 @@ export class FrigateCardViewerCarousel extends FrigateCardMediaCarousel {
} }
} }
protected _playOrPauseClip(action: 'play' | 'pause', slide: HTMLElement): void {
const player = slide.querySelector('frigate-card-ha-hls-player') as
| (HTMLElement & { play: () => void; pause: () => void })
| undefined;
if (player) {
if (action === 'play') {
player.play();
} else if (action === 'pause') {
player.pause();
}
}
}
/** /**
* Play the clip being shown to the user (video player may already be loaded * Pause all clips.
* depending on the lazyload configuration).
*/ */
protected _autoplayHandler(): void { protected _pauseAllHandler(): void {
if (this._carousel) {
this._carousel
.slideNodes()
.forEach((slide) => this._playOrPauseClip('pause', slide));
}
}
/**
* Play the clip being shown to the user and pause the prior.
*/
protected _autoplayPauseHandler(pausePrevious: boolean): void {
if (!this._carousel) { if (!this._carousel) {
return; return;
} }
const nodes = this._carousel.slideNodes();
this._carousel.slidesInView(true).forEach((slide) => { const slides = this._carousel.slideNodes();
const player = nodes[slide].querySelector('frigate-card-ha-hls-player') as
| (HTMLElement & { play: () => void }) // Pause the previous/current slide.
| undefined; if (pausePrevious) {
if (player) { this._carousel
player.play(); .slidesInView(false)
} .forEach((slide) => {
}); this._playOrPauseClip('pause', slides[slide])
});
}
// Play the target slide.
this._carousel
.slidesInView(true)
.forEach((slide) => {
this._playOrPauseClip('play', slides[slide])
});
} }
/** /**
@@ -554,8 +590,9 @@ export class FrigateCardViewerCarousel extends FrigateCardMediaCarousel {
super._initCarousel(); super._initCarousel();
if (this._carousel && this.viewerConfig && this.viewerConfig.autoplay_clip) { if (this._carousel && this.viewerConfig && this.viewerConfig.autoplay_clip) {
this._carousel.on('init', this._autoplayHandler.bind(this)); this._carousel.on('destroy', () => this._pauseAllHandler());
this._carousel.on('select', this._autoplayHandler.bind(this)); this._carousel.on('init', () => this._autoplayPauseHandler(false));
this._carousel.on('select', () => this._autoplayPauseHandler(true));
} }
} }
+8 -3
View File
@@ -29,9 +29,14 @@ customElements.whenDefined('ha-hls-player').then(() => {
* Play the video. * Play the video.
*/ */
public play(): void { public play(): void {
if (this._videoRef.value) { this._videoRef.value?.play();
this._videoRef.value.play(); }
}
/**
* Pause the video.
*/
public pause(): void {
this._videoRef.value?.pause();
} }
// ===================================================================================== // =====================================================================================