【问题标题】:Silent microphone audio getUserMedia静音麦克风音频 getUserMedia
【发布时间】:2017-10-26 16:26:23
【问题描述】:

这两天我一直在碰壁,我真的希望有人能帮助解决这个问题。

我已经从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);

如果有人以前遇到过这种情况,我会非常感激修复。

感谢大家的宝贵时间,祝您度过愉快的一天/一夜

【问题讨论】:

    标签: web webrtc web-deployment microphone getusermedia


    【解决方案1】:

    首先检查您的麦克风与一般recording demo
    如果它正常工作,您可以尝试仅将音频流和所需的 mime 类型传递给媒体记录器以进行基本音频录制。
    如果你想玩这个网络音频上下文,

    我怀疑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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 2013-11-18
      • 1970-01-01
      • 1970-01-01
      • 2018-06-18
      • 1970-01-01
      • 2012-07-31
      相关资源
      最近更新 更多