Search code examples
firefoxgetusermedia

Firefox v41 issue with getUserMedia not see in Chrome or Firefox v36


I use the following code from (https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia):

navigator.mediaDevices = navigator.mediaDevices || ((navigator.mozGetUserMedia || navigator.webkitGetUserMedia) ? {
   getUserMedia: function(c) {
     return new Promise(function(y, n) {
       (navigator.mozGetUserMedia || navigator.webkitGetUserMedia).call(navigator, c, y, n);
     });
   }
} : null);

to setup the microphone for use. This works great in Chrome (v45) and Firefox (v36), but in Firefox (v41) I get the following error in the console:

Error: setting a property that has only a getter
RecorderSvc.initAudio@http://fakewebsite.com/js/services/recorder.js:61:1

I can solve the problem by doing:

if (navigator.mozGetUserMedia || navigator.webkitGetUserMedia) {
    navigator.mediaDevices.getUserMedia = function(c) {
        return new Promise(function(y, n) {
            (navigator.mozGetUserMedia || navigator.webkitGetUserMedia).call(navigator, c, y, n);
        });
    }
}

but this doesn't work in Chrome or Firefox (v36).

  • In Chrome, only navigator.webkitGetUserMedia is defined.
  • In Firefox (v36), only navigator.mozGetUserMedia is defined.
  • In Firefox (v41), both navigator.mozGetUserMedia AND navigator.mediaDevices are defined.

I can't figure out how to fix this without breaking one of the browsers. Any ideas?


Solution

  • If you surround your statement in a Try Catch block, it'll work.

    if (navigator.mediaDevices || (navigator.mozGetUserMedia || navigator.webkitGetUserMedia)) {
      try {
        navigator.mediaDevices = {
          getUserMedia: function(c) {
            return new Promise(function(y, n) {
              (navigator.mozGetUserMedia || navigator.webkitGetUserMedia).call(navigator, c, y, n);
            });
          }
        };
      }
      catch(err) {
        navigator.mediaDevices.getUserMedia = function(c) {
          return new Promise(function(y, n) {
            (navigator.mozGetUserMedia || navigator.webkitGetUserMedia).call(navigator, c, y, n);
          });
        }
      }
    } else {
      navigator.mediaDevices = null;
    }