静音麦克风音频getUserMedia

时间:2017-05-25 21:49:01

标签: web webrtc web-deployment microphone getusermedia

我现在已经把头撞在墙上两天了,我真的希望有人可以帮忙解决这个问题。

我从https://higuma.github.io/wav-audio-encoder-js/ + https://github.com/higuma/wav-audio-encoder-js处获取了一些getUserMedia麦克风录音机的代码。我已经删除了我不需要的组件 - 不知何故,在这个过程中,我已经设法使它在生成的文件中没有音频通过。

看起来它格式正确 - 但是完全无声。我得到0个错误。

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

// audio context + .createScriptProcessor shim
var audioContext = new AudioContext;
if (audioContext.createScriptProcessor == null) {
  audioContext.createScriptProcessor = audioContext.createJavaScriptNode;
}

// selectors
var $microphone = $('#microphone');
var $cancel = $('#cancel');
var $recordingList = $('#recording-list');
var $timeDisplay = $('#time-display');
var $microphoneLevel = $('#microphone-level');

var microphone = undefined;
var input = audioContext.createGain();
var mixer = audioContext.createGain();
var microphoneLevel = audioContext.createGain();
microphoneLevel.gain.value = 0;
microphoneLevel.connect(mixer);
var processor = undefined;
var startTime = null;
var encoder = undefined;

// obtaining microphone input
$microphone.click(function() {
    navigator.getUserMedia({ audio: true },
      function(stream) {
        microphone = audioContext.createMediaStreamSource(stream);
        microphone.connect(microphoneLevel);
        console.log(microphone);
      },
      function(error) {
        window.alert("Could not get audio input");
      });
});

// start/stop recording
$microphone.click(function() {
  if (startTime != null) {
    stopRecording(true);
  } else {
    startRecording();
  }
});

// cancel recording (without saving)
$cancel.click(function() {
  stopRecording(false);
});

// microphone level slider
$microphoneLevel.on('input', function() {
  var level = $microphoneLevel[0].valueAsNumber / 100;
  microphoneLevel.gain.value = level * level;
});

function startRecording() {
  startTime = Date.now();
  $microphone.html('Stop');
  $cancel.removeClass("hidden");
  startRecordingProcess();
}

function startRecordingProcess() {
  processor = audioContext.createScriptProcessor(1024, 2, 2);
  input.connect(processor);
  processor.connect(audioContext.destination);
  // wav encoder
  encoder = new WavAudioEncoder(audioContext.sampleRate, 2);
  processor.onaudioprocess = function(event) {
    encoder.encode(getBuffers(event));
  };
}

function getBuffers(event) {
  var buffers = [];
  for (var ch = 0; ch < 2; ++ch) {
    buffers[ch] = event.inputBuffer.getChannelData(ch);
  }
  return buffers;
}

function stopRecording(finish) {
  startTime = null;
  $timeDisplay.html('00:00');
  $microphone.html('<i class="start fa fa-microphone fa-5x" aria-hidden="true"></i>');
  $cancel.addClass('hidden');
  stopRecordingProcess(finish);
}

function stopRecordingProcess(finish) {
  input.disconnect();
  processor.disconnect();

  if (finish) { // if microphone pressed
    saveRecording(encoder.finish());
  } else { // if cancel pressed
    encoder.cancel();
  }
}

function saveRecording(blob) {
  var url = URL.createObjectURL(blob);
  var html = "<p class='recording' recording='" + url + "'><a class='btn btn-default' href='" + url + "' download='recording.wav'>Save Recording</a></p>";
  $recordingList.prepend($(html));

  // once we have done all the processing, upload the file to beyond verbal
  // uploadFile(blob);
}

// update the recording timer
function minuteSeconds(n) { return (n < 10 ? "0" : "") + n; }
function updateDateTime() {
  if (startTime !== null) {
    var sec = Math.floor((Date.now() - startTime) / 1000);
    $timeDisplay.html(minuteSeconds(sec / 60 | 0) + ":" + minuteSeconds(sec % 60));
  }
}
window.setInterval(updateDateTime, 200);

如果有人之前碰到过这种情况,我真的很感激修复。

谢谢大家的时间,祝你有个美好的一天

1 个答案:

答案 0 :(得分:0)

首先用普通recording demo检查您的麦克风 如果它正常工作,你可以尝试只传递Audio Stream&amp;所需的mime类型到媒体记录器进行基本录音。
如果您想使用此webaudio上下文,

我怀疑microphoneLevel.gain.value = 0;的问题 将其更改为microphoneLevel.gain.value = 1; //or 2

gain = 0表示我们正在静音。 gain = 1默认音频级别
gain = 0.1 - 0.9正在降低音量 gain = above 1.1提高音量

在控制台上打印级别值

// microphone level slider
$microphoneLevel.on('input', function() {
   var level = $microphoneLevel[0].valueAsNumber / 100;
   console.log('value: ' + $microphoneLevel[0].valueAsNumber + ' Level: ' + level);
   microphoneLevel.gain.value = level * level; // if level is zero, then its silent
   // its better if you have a predefined level values based slider position instead of multiplying it
});

查看我的demosource