【问题标题】:Property 'captureStream' does not exist on type 'HTMLVideoElement' [duplicate]“HTMLVideoElement”类型上不存在属性“captureStream”[重复]
【发布时间】:2021-06-19 07:32:09
【问题描述】:

我正在做一个简单的项目,在 React 中使用 WebRTC 和 typescript。

我在关注MDN HTMLMediaElement.captureStream()

const vid: HTMLVideoElement | null = document.querySelector("video");
if (vid) {
   vid.captureStream();
}

.
.
.

<video id="myVid"></video>

但是我收到了这个错误,

Property 'captureStream' does not exist on type 'HTMLVideoElement'.ts(2339)

我什至尝试过,

const vid: HTMLMediaElement | null = document.querySelector("video");

我做错了什么??

编辑

我试过了

const videoCont = useRef<HTMLVideoElement>(null);

var URL = window.URL || window.webkitURL
const fileURL = URL.createObjectURL(e)
if (videoCont.current) {
         videoCont.current.src = fileURL;
         videoCont.current.play = async () => {
              const mediaStream = videoCont.current?.captureStream();
       }
   }

还是同样的错误,

Property 'captureStream' does not exist on type 'HTMLVideoElement'.

编辑 2

我查看了unresolved method captureStream on HTMLCanvasElement

显然,这仍然是一项实验性功能,并非所有浏览器都支持。将不得不等待。

【问题讨论】:

  • 我猜,它只是说这是一个实验性功能。
  • (videoCont.current? as any).captureStream(); 这将使错误消失,mediaelement 上的captureStream 是实验性的,在 TS 中尚不可用

标签: html reactjs typescript webrtc


【解决方案1】:

该属性看起来experimental,可能尚未添加到TS。您可以使用any 或事件制作您自己的类型:

interface HTMLMediaElementWithCaputreStream extends HTMLMediaElement{
  captureStream(): MediaStream;
}

【讨论】:

  • 我尝试创建自己的类型,但 captureStream() 刚刚返回 undefined。我想我将不得不等到 TS 支持这个
  • 上面的代码只会减轻 TS 所做的检查。如果您的浏览器返回未定义,那么它可能不支持此功能。
  • 但是从this链接我们可以看到大多数现代浏览器都支持captureStream(),我正在测试最新版本的FF
  • 嗯。检查它应该是播放还是播放。
【解决方案2】:

captureStream() 方法存在于 HTMLCanvasElements 中,因此请将类型更改为 HTMLCanvasElementany

【讨论】:

  • 那我怎么用videoCont.current.play();或者类似的功能
  • 使用any,我似乎无法更新搜索栏。使用HTMLCanvasElement,我无法使用任何视频播放器功能
猜你喜欢
  • 2021-02-03
  • 2023-03-31
  • 2019-04-06
  • 2021-10-23
  • 1970-01-01
  • 2021-01-09
  • 2021-07-02
  • 2018-09-05
  • 2019-04-11
相关资源
最近更新 更多