What constraints should I pass to getUserMedia() in order to get two video mediaStreamTracks?

后端 未结 1 1607
臣服心动
臣服心动 2020-12-03 16:14

I can get mediaDevices of \'videoinput\' kind via navigator.mediaDevices.enumerateDevices() promise.

I can get mediaStream via navigator.mediaDevi

相关标签:
1条回答
  • 2020-12-03 16:22

    You can get max one video track and one audio track each time you call getUserMedia(), but you can call it multiple times. This may ask the user more than once though, depending on https, browser, and what the user does.

    Following the standard (which at this time requires using adapter.js in Chrome), to get a specific "videoinput" device, pass its deviceId into getUserMedia using the deviceId constraint:

    navigator.mediaDevices.enumerateDevices()
    .then(devices => {
      var camera = devices.find(device => device.kind == "videoinput");
      if (camera) {
        var constraints = { deviceId: { exact: camera.deviceId } };
        return navigator.mediaDevices.getUserMedia({ video: constraints });
      }
    })
    .then(stream => video.srcObject = stream)
    .catch(e => console.error(e));
    

    The exact keyword makes the constraint required, guaranteeing it'll return only the right one, or fail.

    If you want two cameras, you'll have to call getUserMedia again with a different deviceId, and hope the OS you're on supports it (e.g. phones generally don't).

    0 讨论(0)
提交回复
热议问题