navigator.getUserMedia not working on Android / Chrome

前端 未结 2 1654
南旧
南旧 2021-02-07 19:40

do you have any idea of why the following code doesn\'t work on Android/Chrome?

It works well on Desktop/Chrome.

2条回答
  •  北恋
    北恋 (楼主)
    2021-02-07 20:15

    According to MDN, navigator.getUserMedia() is deprecated and isn't supported on Android/Chrome and some newer browser versions. Use navigator.mediaDevices.getUserMedia() instead. You can check browser compatibility below.

    MDN Navigator.getUserMedia browser check

    Here's a partial example I've used to access the camera for video streaming in a past project. The browser should ask the user for access on the device.

    if(navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
      navigator.mediaDevices.getUserMedia({ audio: false, video:  cameraOrientation })
        .then(function(stream) {
          if ("srcObject" in video) {
              video.srcObject = stream;
            } else {
              video.src = window.URL.createObjectURL(stream);
            }
            video.onloadedmetadata = function(e) {
              video.play();
            };
        });
    };
    

提交回复
热议问题