如何在javascript中捕获音频? [英] How to capture audio in javascript?
问题描述
我目前正在使用 getUserMedia()
,这只适用于Firefox和Chrome,但它已被弃用,仅适用于https(在Chrome中)。有没有其他/更好的方法来获得适用于所有平台的javascript中的语音输入?
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?
例如。 web.whatsapp.com app等网站如何录制音频? getUserMedia()
提示首次用户允许录音,而Whatsapp应用程序不需要用户的许可。
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.
getUserMedia()
我目前使用的是这样的:
The getUserMedia()
I am currently using looks like this:
navigator.getUserMedia(
{
"audio": {
"mandatory": {
"googEchoCancellation": "false",
"googAutoGainControl": "false",
"googNoiseSuppression": "false",
"googHighpassFilter": "false"
},
"optional": []
},
}, gotStream, function(e) {
console.log(e);
});
推荐答案
Chrome 60+执行需要使用 https ,因为 getUserMedia
功能强大功能。 API访问不应在非安全域中工作,因为该API访问可能会流失到非安全的actor。 Firefox仍然支持 getUserMedia
而不是 http 。
Chrome 60+ does require using https, since getUserMedia
is 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 getUserMedia
over http, though.
我一直在使用< a href =https://github.com/mattdiamond/Recorderjs =nofollow noreferrer> RecorderJS ,它完全符合我的目的。
这是一个代码示例。 (来源)
I've been using RecorderJS and it served my purposes well. Here is a code example. (source)
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;
};
}
用法很简单:
var recorder = new RecordAudio(userMedia);
recorder.start();
recorder.stop();
var recordedData = recorder.getData()
编辑:你可能还想查看这个如果无效则回答。
You may also want to check this answer if nothing works.
这篇关于如何在javascript中捕获音频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!