Don't use createRef for patched players.

This commit is contained in:
Dermot Duffy
2022-05-10 12:38:05 -07:00
parent 495170561f
commit cf87e8543a
3 changed files with 46 additions and 29 deletions
+18 -10
View File
@@ -9,43 +9,54 @@
// available as compilation time.
// ====================================================================
import { Ref, createRef, ref } from 'lit/directives/ref.js';
import { TemplateResult, html } from 'lit';
import { customElement } from 'lit/decorators.js';
import { query } from 'lit/decorators/query.js';
import { dispatchErrorMessageEvent, dispatchMediaShowEvent } from '../common.js';
customElements.whenDefined('ha-web-rtc-player').then(() => {
@customElement('frigate-card-ha-web-rtc-player')
// eslint-disable-next-line @typescript-eslint/no-unused-vars
class FrigateCardHaWebRtcPlayer extends customElements.get('ha-web-rtc-player') {
protected _videoRef: Ref<HTMLVideoElement> = createRef();
// Due to an obscure behavior when this card is casted, this element needs
// to use query rather than the ref directive to find the player.
@query('#remote-stream')
protected _video: HTMLVideoElement;
/**
* Play the video.
*/
public play(): void {
this._videoRef.value?.play();
this._video?.play();
}
/**
* Pause the video.
*/
public pause(): void {
this._videoRef.value?.pause();
this._video?.pause();
}
/**
* Mute the video.
*/
public mute(): void {
this.muted = true;
// The muted property is only for the initial muted state. Must explicitly
// set the muted on the video player to make the change dynamic.
if (this._video) {
this._video.muted = true;
}
}
/**
* Unmute the video.
*/
public unmute(): void {
this.muted = false;
// See note in mute().
if (this._video) {
this._video.muted = false;
}
}
// =====================================================================================
@@ -56,13 +67,10 @@ customElements.whenDefined('ha-web-rtc-player').then(() => {
if (this._error) {
// Use native Frigate card error handling, and attach the entityid to
// clarify which camera the error refers to.
return dispatchErrorMessageEvent(
this,
`${this._error} (${this.entityid})`);
return dispatchErrorMessageEvent(this, `${this._error} (${this.entityid})`);
}
return html`
<video
${ref(this._videoRef)}
id="remote-stream"
?autoplay=${this.autoPlay}
.muted=${this.muted}