jquery 导航器.mediaDevices.getUserMedia非法调用错误

huwehgph  于 2023-03-17  发布在  jQuery
关注(0)|答案(1)|浏览(127)

我在一个项目中使用这段代码,我正在工作,以记录一个音频文件在用户端,然后将其直接上传到服务器所有从一个页面...由于某种原因,我得到了一个非法调用错误,当我运行这段代码。我知道这意味着有一个上下文错误,但我不太清楚它是从哪里来的...

navigator.mediaDevices.getUserMedia({
    audio: true
  })
  .then(function(stream) {
    recorder = new MediaRecorder(stream);

    const chunks = [];

    recorder.onstop = (e) => {
      const blob = new Blob(chunks, {
        type: "audio/wav;codecs=MS_PCM"
      });
      var fileUpload = new FormData();
      fileUpload.append("fileToUpload", blob, "audio.wav");
      $.post("upload.php", fileUpload);
    };

    recorder.ondataavailable = (e) => {
      chunks.push(e.data);
    };

    recorder.start();
  });

如果有人有任何想法...他们显然会很感激!

wtlkbnrh

wtlkbnrh1#

我发现这个错误来自于在jQuery上下文中对事件“e.data“变量的调用。做一个简单 AJAX XHR很快就解决了这个问题...

const xhr = new XMLHttpRequest();
xhr.open("POST", 'upload.php', true);
xhr.send(fileUpload);

把它放在一起...它现在工作没有错误!

navigator.mediaDevices.getUserMedia({
    audio: true
  })
  .then(function(stream) {
    audioStream = stream;
    recorder = new MediaRecorder(stream);

    const chunks = [];

    recorder.onstop = (e) => {
      const blob = new Blob(chunks, {
        type: "audio/wav;codecs=MS_PCM"
      });
      var fileUpload = new FormData();
      fileUpload.append("fileToUpload", blob, "audio.wav");
      const xhr = new XMLHttpRequest();
      xhr.open("POST", 'upload.php', true);
      xhr.send(fileUpload);
    };

    recorder.ondataavailable = (e) => {
      chunks.push(e.data);
    };

    recorder.start();
  });

相关问题