如何使用javascript录制音频元素的音频

时间:2015-06-18 08:13:53

标签: javascript html5 audio audio-recording getusermedia

我正在使用HTML5和Javascript制作录音机,并且不想包含任何第三方API,我在第一步就是使用<audio>标签创建了一个音频检索器和播放器navigator.webkitGetUserMedia从我的麦克风获取音频并通过<audio>元素播放的功能,但此时我无法获取阵列中的音频数据我不知道该怎么做才能使用。

1 个答案:

答案 0 :(得分:2)

简单只需创建一个音频节点,下面是来自MattDiamond的RecorderJS的调整代码:

$('dropdown').css( "background-color: #000000" );

示例用法:

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

}