Web Audio API,Recorder.js:无延迟地从sourceBuffer录制

时间:2016-01-10 23:45:43

标签: html5 web-audio recorder.js

我正在尝试创建基于Web Audio API的应用程序。到目前为止,我有多个缓冲节点,连接到目的地。

我想要实现的是能够在用户(例如)按下按钮时记录输出结果,我尝试使用recorder.js,但据我所知,您需要一起播放图形。

以下代码描述了该问题:

<html><body><audio controls autoplay></audio>
<script type="text/javascript" src="recorder.js"> </script>
<input onclick="startRecording()" type="button" value="start recording" />
<input onclick="stopRecording()" type="button" value="stop recording and play" />

<script>

var context = new webkitAudioContext();
var request = new XMLHttpRequest();

var onFail = function(e) {
  console.log('Rejected!', e);
};

var onSuccess = function(s) {
  request.open('GET', 'voice.wav', true);
  request.responseType = 'arraybuffer';

  request.onload = function () {
    var undecodedAudio = request.response;
    context.decodeAudioData(undecodedAudio, function (buffer) {
      // The contents of our mp3 is now an AudioBuffer
      var sourceBuffer = context.createBufferSource();
      sourceBuffer.buffer = buffer;
      sourceBuffer.connect (context.destination);
      recorder = new Recorder(sourceBuffer);
      recorder.record();
      sourceBuffer.start (context.currentTime);
    });
  };

  request.send();
}

window.URL = window.URL || window.webkitURL;
navigator.getUserMedia  = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;

var recorder;
var audio = document.querySelector('audio');

function startRecording() {
  if (navigator.getUserMedia) {
    navigator.getUserMedia({audio: true}, onSuccess, onFail);
  } else {
    console.log('navigator.getUserMedia not present');
  }
}

function stopRecording() {
  recorder.stop();
  recorder.exportWAV(function(s) {
    audio.src = window.URL.createObjectURL(s);
  });
}

</script></body></html>

我想要的是当用户按下录音时,文件被转换为合成音频而不实际播放。

0 个答案:

没有答案