Javascript 如何在javascript中捕获音频?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34820578/
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 16:54:20  来源:igfitidea点击:

How to capture audio in javascript?

javascriptaudiospeechgetusermediavoice-recording

提问by user2212461

I am currently using getUserMedia(), which is only working on Firefox and Chrome, yet it got deprecated and works only on https (in Chrome). Is there any other/better way to get the speech input in javascript that works on all platforms?

我目前正在使用getUserMedia(),它仅适用于 Firefox 和 Chrome,但它已被弃用并且仅适用于 https(在 Chrome 中)。有没有其他/更好的方法可以在适用于所有平台的 javascript 中获取语音输入?

E.g. how do websites like web.whatsapp.com app record audio? getUserMedia()prompts first-time-users to permit audio recording, whereas the Whatsapp application doesn't require the user's permission.

例如,像 web.whatsapp.com 应用程序这样的网站如何录制音频?getUserMedia()提示首次用户允许录音,而 Whatsapp 应用程序不需要用户的许可。

The getUserMedia()I am currently using looks like this:

getUserMedia()我目前正在使用这个样子的:

navigator.getUserMedia(
    {
        "audio": {
            "mandatory": {
                "googEchoCancellation": "false",
                "googAutoGainControl": "false",
                "googNoiseSuppression": "false",
                "googHighpassFilter": "false"
            },
            "optional": []
        },
    }, gotStream, function(e) {
        console.log(e);
    });

回答by George Netu

Chrome 60+ does require using https, since getUserMediais a powerful feature. The API access shouldn't work in non-secure domains, as that API access may get bled over to non-secure actors. Firefox still supports getUserMediaover http, though.

Chrome 60+ 确实需要使用https,因为它getUserMedia是一个强大的功能。API 访问不应在非安全域中工作,因为该 API 访问可能会转移到非安全参与者。不过,Firefox 仍然支持getUserMedia通过http

I've been using RecorderJSand it served my purposes well. Here is a code example. (source)

我一直在使用RecorderJS,它很好地满足了我的目的。这是一个代码示例。(来源

function RecordAudio(stream, cfg) {

  var config = cfg || {};
  var bufferLen = config.bufferLen || 4096;
  var numChannels = config.numChannels || 2;
  this.context = stream.context;
  var recordBuffers = [];
  var recording = false;
  this.node = (this.context.createScriptProcessor ||
    this.context.createJavaScriptNode).call(this.context,
    bufferLen, numChannels, numChannels);

  stream.connect(this.node);
  this.node.connect(this.context.destination);

  this.node.onaudioprocess = function(e) {
    if (!recording) return;
    for (var i = 0; i < numChannels; i++) {
      if (!recordBuffers[i]) recordBuffers[i] = [];
      recordBuffers[i].push.apply(recordBuffers[i], e.inputBuffer.getChannelData(i));
    }
  }

  this.getData = function() {
    var tmp = recordBuffers;
    recordBuffers = [];
    return tmp; // returns an array of array containing data from various channels
  };

  this.start() = function() {
    recording = true;
  };

  this.stop() = function() {
    recording = false;
  };
}

The usage is straightforward:

用法很简单:

var recorder = new RecordAudio(userMedia);
recorder.start();
recorder.stop();
var recordedData = recorder.getData()

Edit: You may also want to check this answerif nothing works.

编辑:如果没有任何效果,您可能还想检查这个答案

回答by Sasi Varunan

Recorder JS does the easy job for you. It works with Web Audio API nodes

Recorder JS 为您完成了简单的工作。它适用于 Web Audio API 节点

Chrome and Firefox Browsers has evolved now. There is an inbuilt MediaRecoderAPI which does audio recording for you.

Chrome 和 Firefox 浏览器现在已经发展。有一个内置的MediaRecoderAPI 可以为您进行录音。

navigator.mediaDevices.getUserMedia({audio:true})
    .then(stream => {
        rec = new MediaRecorder(stream);
        rec.ondataavailable = e => {
            audioChunks.push(e.data);
            if (rec.state == "inactive"){
               // Use blob to create a new Object URL and playback/download
            }
        }
    })
    .catch(e=>console.log(e));

Working demo

工作演示

MediaRecodersupport starts from

MediaRecoder支持始于

Chrome support: 47

Chrome 支持:47

Firefox support: 25.0

火狐支持:25.0

回答by LJ Wadowski

The getUserMedia()isn't deprecated, deprecated is using it over http. How far I know the only browser which requires httpsfor getUserMedia()right now is Chrome what I think is correct approach.

getUserMedia()不会被弃用,被弃用了使用它http。多远我知道这需要唯一的浏览器httpsgetUserMedia()现在的问题是Chrome的什么,我认为是正确的做法。

If you want ssl/tlsfor your test you can use free version of CloudFlare.

如果您想ssl/tls进行测试,可以使用 CloudFlare 的免费版本。

Whatsapp page doesn't provide any recording functions, it just allow you to launch application.

Whatsapp 页面不提供任何录音功能,它只允许您启动应用程序。

Good article about getUserMedia

关于的好文章 getUserMedia

Fully working example with use of MediaRecorder

使用完整的工作示例 MediaRecorder