Javascript Firefox:navigator.getUserMedia 不是一个函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28991835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:42:55  来源:igfitidea点击:

Firefox: navigator.getUserMedia is not a function

javascriptfirefoxhtml5-audio

提问by realtebo

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

但是,使用最新的 FF 可用我得到一个 javascript 错误,说

navigator.getUserMedia is not a function

navigator.getUserMedia 不是一个函数

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

我从这篇博文中复制了这段代码:https: //blog.groupbuddies.com/posts/39-tutorial-html-audio-capture-streaming-to-nodejs-no-browser-extensions

And similar on latest Chrome:

和最新的 Chrome 类似:

Uncaught TypeError: undefined is not a function

未捕获的类型错误:未定义不是函数

But I know that this api is supported from both browser

但我知道这两个浏览器都支持这个 api

What am I doing wrong?

我究竟做错了什么?

采纳答案by Uriel Acosta Hernández

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

Navigator.getUserMedia() 已弃用,建议使用 MediaDevices.getUserMedia()

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

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

回答by Scimonster

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

尚不支持无前缀。见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.getUserMediato whatever it detects to be the proper prefixed version.

这将设置navigator.getUserMedia为它检测到的正确前缀版本。

回答by Subin

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

由于navigator.getUserMedia()已弃用,请使用:

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);
}

回答by Martijn Arts

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

检查MDN 页面,尤其是第一个示例的第一部分:

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

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

它没有得到很好的支持 - 您需要浏览器前缀。