how to make getUserMedia() work on all browsers

后端 未结 5 418
执念已碎
执念已碎 2020-11-29 22:39

I learn about web-rtc, it says that you can capture video-cam , i used demo , well this worked on chrome only..

when i open it on firefox i get mess

相关标签:
5条回答
  • 2020-11-29 23:04

    Since Safari 11 is out, this works everywhere (tested on recent versions of Chrome, Firefox and Safari 11):

    var constraints = {audio: false, video: true};
    var video = document.querySelector("video");
    
    function successCallback(stream) {
      video.srcObject = stream;
      video.play();
    }
    
    function errorCallback(error) {
      console.log("navigator.getUserMedia error: ", error);
    }
    
    navigator.mediaDevices.getUserMedia(constraints)
      .then(successCallback)
      .catch(errorCallback);
    
    0 讨论(0)
  • 2020-11-29 23:06

    getUserMedia needs to be prefixed with webkit- or moz-. The first example is only prefixed with webkit-. Therefor it will never work on Firexox, only Chrome and Safari.

    The code example does not show the prefix...

    Prefixing can be done in this way:

    navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
    
    0 讨论(0)
  • 2020-11-29 23:08
    navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
    

    In addition, you have to use https instead of http, otherwise Safari for iPadOS won't work.

    0 讨论(0)
  • 2020-11-29 23:13

    Fiddles

    navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
    
    var constraints = {audio: false, video: true};
    var video = document.querySelector("video");
    
    function successCallback(stream) {
      window.stream = stream; // stream available to console
      if (window.URL) {
        video.src = window.URL.createObjectURL(stream);
      } else {
        video.src = stream;
      }
    }
    
    function errorCallback(error){
      console.log("navigator.getUserMedia error: ", error);
    }
    
    navigator.getUserMedia(constraints, successCallback, errorCallback);
    
    0 讨论(0)
  • 2020-11-29 23:23

    The issue is not just the prefixed function name; the stream provided works differently in different browsers. Here, I'll walk you through it.

    I assume you've already set up a video element in the variable called video.

    //I don't usually like to overwrite publicly accessible variables, but that's just me
    var getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
    var cameraStream;
    
    getUserMedia.call(navigator, {
        video: true,
        audio: true //optional
    }, function (stream) {
        /*
        Here's where you handle the stream differently. Chrome needs to convert the stream
        to an object URL, but Firefox's stream already is one.
        */
        if (window.webkitURL) {
            video.src = window.webkitURL.createObjectURL(stream);
        } else {
            video.src = stream;
        }
    
        //save it for later
        cameraStream = stream;
    
        video.play();
    });
    

    This should cover you for Firefox, Chrome and Opera. IE and Safari don't support it yet.

    Another potentially annoying thing to be aware of is how to turn off the camera if you want to stop using it before leaving the web page. Use this function:

    function stopWebCam() {
        if (video) {
            video.pause();
            video.src = '';
            video.load();
        }
    
        if (cameraStream && cameraStream.stop) {
            cameraStream.stop();
        }
        stream = null;
    }
    
    0 讨论(0)
提交回复
热议问题