28

I get the warning that a function will be deprecated in Chrome future release.

It's this script:

navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia || navigator.oGetUserMedia;
if (navigator.getUserMedia) {
    navigator.getUserMedia({
        video: true
    }, (stream) => {
        this.src = window.URL.createObjectURL(stream);
        this.stream = stream;
    }, (error) => {
        console.log(error);
    });
}

That records webcam so I can save it, but the following warning is shown in the console:

[Deprecation] URL.createObjectURL with media streams is deprecated and will be removed in M68, around July 2018. Please use HTMLMediaElement.srcObject instead.

But when I change:

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

To

this.src = window.HTMLMediaElement.srcObject(stream);

It doesn't work anymore like it did before..

3 Answers 3

64
+150

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

if ('srcObject' in this) {
  this.srcObject = stream;
} else {
  this.src = window.URL.createObjectURL(stream);
}

This is taken from Mozilla Documentation

2
  • this doesn't answer the question for me. createObjectURL was not only used for displaying video, it was also used for downloading files. the src tag does not accept a srcObject, now there is no solution for downloading a file.
    – tatsu
    Commented Nov 19, 2019 at 10:35
  • @tatsu What are you talking about the answer is correct for the question asked if it does not work for you open a question and give us your code to see what is wrong with it, don't just say does not answer the question for me, well then you have a different question.
    – Barkermn01
    Commented Nov 20, 2019 at 13:43
9

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

2
  • the <src DOM tag does not accept the srcObject value.
    – tatsu
    Commented Nov 19, 2019 at 10:32
  • how do you turn the result of a FileReader() into a stream Commented Mar 23, 2022 at 20:34
0

If you are using Chrome you can use:

video.srcObject = stream;

instead of:

this.srcObject = stream;
1
  • 1
    only if video variable is setup with the scopes required and points to the video element you're wanting to effect at no point in the OP's code is video defined
    – Barkermn01
    Commented Jul 18, 2019 at 10:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.