Deprecation of createObjectURL and replace with the new HTMLMediaElement.srcObject doesn't work for Webcam stream

情到浓时终转凉″ 提交于 2019-12-03 01:29:27

Your misunderstanding what HTMLMediaElement is.

It is the JavaScript Class/Prototype that represents a HTML <audio> or <video> tag whether it's in the HTML or not.

For a more class like explanation <audio> on the page is an object of type HTMLAudioElement and that extends HTMLMediaElement and that in turn extends HTMLElement.

If you get the media element with querySelector() or getElementById() or create a media element in JavaScript with createElement("audio") or createElement("video") you'll get an instance of HTMLMediaElement.

In your case this is an object of HTMLMediaElement class.

With JavaScript, as a rule of thumb if the object type name starts with HTML it is referring to an HTML Element / Tag.

All you need to do is change

this.src = window.URL.createObjectURL(stream);

to

try {
  this.srcObject = stream;
} catch (error) {
  this.src = window.URL.createObjectURL(stream);
}

This is taken from Mozilla Documentation

You can read more about how this change should be used, and where this answer takes knowledge from: https://www.fxsitecompat.com/en-CA/docs/2017/url-createobjecturl-stream-has-been-deprecated/

Replacing this.src = window.URL.createObjectURL(stream); by this.srcObject = stream; should fix the problem.

Fernando Rivero

If you are using Chrome you can use:

video.srcObject = stream;

instead of:

this.srcObject = stream;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!