navigator.mediaDevices deviceId not working on Flutter

huangapple go评论58阅读模式
英文:

navigator.mediaDevices deviceId not working on Flutter

问题

我正在使用Flutter制作一个视频通话应用,目前我正在使用以下代码获取视频流:

Future<MediaStream> _getLocalVideoStream(dynamic navigator) async {
    _loadCamerasInfo();
    return navigator.mediaDevices.getUserMedia({
        "video": {"deviceId":"0"},
        "audio": false
    });
}

这应该打开后置摄像头,但实际上却打开了前置摄像头。我还检查了所有媒体设备,如下所示:

List<MediaDeviceInfo> deviceInfo =
        await navigator.mediaDevices.enumerateDevices();

deviceInfo的每个项目的deviceId属性上迭代会得到以下值:0,1,2,3,这意味着0应该对应于其中一个摄像头,但无论我在_getLocalVideoStream函数中使用此列表中的哪个ID,都会使用默认的前置摄像头。

我该如何纠正这个问题,以便可以循环使用设备上的不同摄像头?(我正在真实的Android设备上测试这个)

英文:

I am making an app with video calls using flutter, I am currently getting the video stream using the following code:

 Future&lt;MediaStream&gt; _getLocalVideoStream(dynamic navigator) async {
    _loadCamerasInfo();
    return navigator.mediaDevices.getUserMedia({
      &quot;video&quot;: {&quot;deviceId&quot;:&quot;0&quot;},
      &quot;audio&quot;: false
    });
  }

This should open the back facing camera but the front facing camera is opened instead, I have also checked all of the media devices like so:

List&lt;MediaDeviceInfo&gt; deviceInfo =
        await navigator.mediaDevices.enumerateDevices();

Iterating over the deviceId attribute of each item in deviceInfo results in the following values: 0,1,2,3 which means that 0 should correspond to one of the cameras however no matter which id I use from this list in the _getLocalVideoStream function the same default front facing camera is used instead.

How can I correct this so I can cycle through the different cameras of the device? (I am testing this on a real Android device)

答案1

得分: 2

First off, when you use navigator.mediaDevices.enumerateDevices(); it will return all mediaDevices. This includes microphones, headsets and cameras. So it is possible that deviceId 0 is not a camera and could be a microphone.

首先,当您使用 navigator.mediaDevices.enumerateDevices(); 时,它将返回所有的媒体设备,包括麦克风、耳机和摄像头。所以设备ID 0 可能不是摄像头,而是麦克风。

Secondly when you list your constraints:

其次,当您列出您的约束条件:

{
"video": {"deviceId":"0"},
"audio": false
}

This is not a respected constraint for the device Id, meaning that it could possibly ignore your request for the device Id and just pick the first video device it finds. You can modify the constraints to look like below:

这不是设备ID的受尊重的约束条件,这意味着它可能会忽略您对设备ID的请求,只是选择它找到的第一个视频设备。您可以修改约束条件如下:

{
"video": {
"deviceId": {
"exact": someId
}
}
}

Using the exact keyword will force it to use that Id. There is a helpful page that goes over all of this here.

使用 "exact" 关键字将强制使用该ID。有一个有关所有这些内容的有用页面 here

Further more, if you don't want to search for a specific deviceId and use that. There is a helpful keyword you can use called 'facingMode', this is primarily for mobile devices. It can be used to set a preference for what type of camera you would like. For example facing the user, or facing the environment. Please take a look at the example below:

此外,如果您不想搜索特定的设备ID并使用它,还有一个有用的关键字可以使用,称为 'facingMode',这主要是针对移动设备的。它可用于设置您想要的摄像头类型的首选项,例如面向用户或面向环境。请查看下面的示例:

"video": {
"facingMode": "user" //prefers front camera if available
// or "facingMode": "environment" --> prefers rear camera if available
}

英文:

First off, when you use navigator.mediaDevices.enumerateDevices(); it will return all mediaDevices. This includes microphones, headsets and cameras. So it is possible that deviceId 0 is not a camera and could be a microphone.

Secondly when you list your constraints:

{
  &quot;video&quot;: {&quot;deviceId&quot;:&quot;0&quot;},
  &quot;audio&quot;: false
}

This is not a respected constraint for the device Id, meaning that it could possibly ignore your request for the device Id and just pick the first video device it finds. You can modify the constraints to look like below:

{
    &quot;video&quot;: {
               &quot;deviceId&quot;: {
                  &quot;exact&quot;: someId
               }
             }
}

Using the exact keyword will force it to use that Id. There is a helpful page that goes over all of this here

Further more, if you don't want to search for a specific deviceId and use that. There is a helpful keyword you can use called 'facingMode', this is primarily for mobile devices. It can be used to set a preference for what type of camera you would like. For example facing the user, or facing the environment. Please take a look at the example below:

&quot;video&quot;: {
           &quot;facingMode&quot;: &quot;user&quot; //prefers front camera if available
//      or &quot;facingMode&quot;: &quot;environment&quot;  --&gt; perfers rear camera if available
         }

huangapple
  • 本文由 发表于 2023年3月21日 02:03:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75793804.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定