[英]How to record audio from Audio Element using javascript
我正在制作一个使用HTML5和Javascript的录音机,并且不想包含任何第三方API,我在第一步就是通过使用<audio>
标签创建音频检索器和播放器以及从我的麦克风获取音频的navigator.webkitGetUserMedia
函数并通过<audio>
元素播放,但此时我无法获取数组中的音频数据我不知道该怎么做才能使用。
simple just create a audio node, below is tweaked code from MattDiamond's RecorderJS: 简单只需创建一个音频节点,下面是来自MattDiamond的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;
};
}
example usage: 示例用法:
var recorder = new RecordAudio(userMedia);
recorder.start();
recorder.stop();
var recordedData = recorder.getData();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.