未捕获(承诺)DOMException:用户中止了请求

时间:2019-12-05 12:57:46

标签: javascript jquery html audio

当我尝试为音频记录器添加模块bitcrusher时,此错误(未捕获(承诺)DOMException:用户中止请求)弹出,模块无法正常工作。

来自https://webaudio.github.io/web-audio-api/#the-bitcrusher-node的Bitcrusher信息是问题

我需要这些规格用于音频输出:

WAV容器
编码PCM
评分16K
样本格式16位
单声道频道

  URL = window.URL || window.webkitURL;
  var gumStream;
  //stream from getUserMedia()
  var rec;
  //Recorder.js object

  var input;
  //MediaStreamAudioSourceNode we'll be recording
  // shim for AudioContext when it's not avb.

  //new audio context to help us record
  var recordButton = document.getElementById("recordButton");
  var stopButton = document.getElementById("stopButton");
  var pauseButton = document.getElementById("pauseButton");

  //add events to those 3 buttons
  recordButton.addEventListener("click", startRecording);
  stopButton.addEventListener("click", stopRecording);
  pauseButton.addEventListener("click", pauseRecording);



 function startRecording() {

  var AudioContext = window.AudioContext || window.webkitAudioContext;

  var audioContext = new AudioContext({
    sampleSize: 16,
    channelCount: 1,
    sampleRate: 16000
  });



   console.log(audioContext.audioWorklet.addModule("bitcrusher.js"))

  console.log("recordButton clicked");

  /* Simple constraints object, for more advanced audio features see

https://addpipe.com/blog/audio-constraints-getusermedia/ */

  var constraints = {
    audio: {
      sampleSize: 16,
      channelCount: 1,
      sampleRate: 16000
  },
  video: false
  };
  /* Disable the record button until we get a success or fail from getUserMedia() */

  recordButton.disabled = true;
  stopButton.disabled = false;
  pauseButton.disabled = false;

  /* We're using the standard promise based getUserMedia()

https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia */

  navigator.mediaDevices
    .getUserMedia(constraints)
    .then(function(stream) {
      console.log(
        "getUserMedia() success, stream created, initializing Recorder.js ..."
      );
      /* assign to gumStream for later use */
      gumStream = stream;
      /* use the stream */
      input = audioContext.createMediaStreamSource(stream);
      /* Create the Recorder object and configure to record mono sound (1 channel) Recording 2 channels will double the file size */
      rec = new Recorder(input, {
        sampleSize: 16,
        numChannels: 1,
        channelCount: 1,
        sampleRate: 16000
      });
      console.log(rec)
      //start the recording process
      rec.record();
      console.log("Recording started");
    })
    .catch(function(err) {
      //enable the record button if getUserMedia() fails
      recordButton.disabled = false;
      stopButton.disabled = true;
      pauseButton.disabled = true;
    });

}

function pauseRecording() {
  console.log("pauseButton clicked rec.recording=", rec.recording);
  if (rec.recording) {
    //pause
    rec.stop();
    pauseButton.innerHTML = "Resume";
  } else {
    //resume
    rec.record();
    pauseButton.innerHTML = "Pause";
  }
}

function stopRecording() {
  console.log("stopButton clicked");
  //disable the stop button, enable the record too allow for new recordings
  stopButton.disabled = true;
  recordButton.disabled = false;
  pauseButton.disabled = true;
  //reset button just in case the recording is stopped while paused
  pauseButton.innerHTML = "Pause";
  //tell the recorder to stop the recording
  rec.stop(); //stop microphone access
  gumStream.getAudioTracks()[0].stop();
  //create the wav blob and pass it on to createDownloadLink
  rec.exportWAV(createDownloadLink);
}

function createDownloadLink(blob) {
  var url = URL.createObjectURL(blob);
  var au = document.createElement("audio");
  var li = document.createElement("li");
  var link = document.createElement("a");
  //add controls to the <audio> element
  au.controls = true;
  au.src = url;
  //link the a element to the blob
  link.href = url;
  // link.download = new Date().toISOString() + '.wav';
  link.innerHTML = link.download;
  //add the new audio and a elements to the li element
  li.appendChild(au);
  li.appendChild(link);
  //add the li element to the ordered list
  recordingsList.appendChild(li);
  var p = document.createElement("br");
  recordingsList.appendChild(p);
}```

0 个答案:

没有答案