Firefox: navigator.getUserMedia is not a function

I'm playing with browser and audio.

I'm doing this

        var session = {
          audio: true,
          video: false
        };
        var recordRTC = null;
        navigator.getUserMedia(session, initializeRecorder, onError);

But, using latest FF available I got a javascript error, saying that

navigator.getUserMedia is not a function

I copied this code from this blog post: https://blog.groupbuddies.com/posts/39-tutorial-html-audio-capture-streaming-to-nodejs-no-browser-extensions

And similar on latest Chrome:

Uncaught TypeError: undefined is not a function

But I know that this api is supported from both browser

What am I doing wrong?

Answers:

Answer

Navigator.getUserMedia() is deprecated, advised to use MediaDevices.getUserMedia()

async function getMedia(pc) {
  let stream = null;

  try {
    stream = await navigator.mediaDevices.getUserMedia(constraints);
    /* use the stream */
  } catch(err) {
    /* handle the error */
  }
}
Answer

It's not supported unprefixed yet. See http://caniuse.com/#search=getusermedia

You'll need to get the browser-specific prefix and use that. As posted in another answer:

navigator.getUserMedia = ( navigator.getUserMedia ||
                       navigator.webkitGetUserMedia ||
                       navigator.mozGetUserMedia ||
                       navigator.msGetUserMedia);

This will set navigator.getUserMedia to whatever it detects to be the proper prefixed version.

Answer

Check the MDN page, especially the first part of the first example:

navigator.getUserMedia = ( navigator.getUserMedia ||
                       navigator.webkitGetUserMedia ||
                       navigator.mozGetUserMedia ||
                       navigator.msGetUserMedia);

It's not that well-supported - you'll need browser prefixes.

Answer

Since navigator.getUserMedia() is deprecated, use this :

navigator.getUserMedia = (
    navigator.getUserMedia ||
    navigator.webkitGetUserMedia ||
    navigator.mozGetUserMedia ||
    navigator.msGetUserMedia
);

if (typeof navigator.mediaDevices.getUserMedia === 'undefined') {
    navigator.getUserMedia({
        audio: true
    }, streamHandler, errorHandler);
} else {
    navigator.mediaDevices.getUserMedia({
        audio: true
    }).then(streamHandler).catch(errorHandler);
}

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.