Andy Gonzalez
Andy Gonzalez

Reputation: 96

Switching facingMode with applyConstraints not CURRENTLY supported (Media Stream Api)

I am reading this article and almost halfway through it, you can read about the following

function switchCameras(track, camera) {
  let constraints = track.getConstraints();
  constraints.facingMode = camera;
  track.applyConstraints(constraints);
}

This function accepts a MediaStreamTrack and a string indicating the camera facing mode to use, fetches the current constraints, sets the value of the MediaTrackConstraints.facingMode to the specified value, then applies the updated constraint set.

But when giving it a try on the live demo at the end of the article, I am trying to change the facingMode videoConstraint on a MediaStreamTrack initiated with getUserMedia while streaming using track.applyConstraints(constraints);

For example, if started with getUserMedia() on these constraints:

{
  video: {
    "facingMode": "user"
  },
  audio: false
}

and using applyConstraints() with this configuration:

{
    "facingMode": { "exact": "environment" }
}

the output is an error

References to these issues are found within this article and this forum bug report. So my question is, is this an expected behavior or are the help on those two and the first article outdated?

Additionally I would greatly appreciate any advice on how to correctly switch cameras using the applyConstraints()

Thanks in advance

Upvotes: 2

Views: 2674

Answers (2)

O. Jones
O. Jones

Reputation: 108816

You can't switch a MediaStream from a front to a back camera, or vice versa. To do that you need to open a new stream with getUserMedia.

Upvotes: 1

kenji tanaka
kenji tanaka

Reputation: 491

I think the reason why the facingMode error is because it is a different device.

Upvotes: 1

Related Questions