简体   繁体   English

从javascript录制和上传音频

[英]Recording and uploading audio from javascript

I am trying to record and upload audio from javascript.我正在尝试从 javascript 录制和上传音频。 I can successfullly record audio Blob s from a MediaRecorder .我可以从MediaRecorder成功录制音频Blob My understanding is that after recording several chunks into blobs, I would concatenate them as a new Blob(audioBlobs) and upload that.我的理解是,在将几个块记录到 blob 中后,我会将它们连接为一个new Blob(audioBlobs)并上传。 Unfortunately, the result on the server-side keeps being more or less gibberish.不幸的是,服务器端的结果或多或少都是胡言乱语。 I'm currently running a localhost connection, so converting to uncompressed WAV isn't a problem (might be come one later, but that's a separate issue).我目前正在运行本地主机连接,因此转换为未压缩的 WAV 不是问题(可能稍后会出现,但这是一个单独的问题)。 Here is what I have so far这是我到目前为止所拥有的

navigator.mediaDevices.getUserMedia({audio: true, video: false})
.then(stream => {
  const mediaRecorder = new MediaRecorder(stream);
  mediaRecorder.start(1000);
  const audioChunks = [];

  mediaRecorder.addEventListener("dataavailable", event => {
    audioChunks.push(event.data);
  });
  
  function sendData () {
    const audioBlob = new Blob(audioChunks);
    session.call('my.app.method', [XXXXXX see below XXXXXX])
  }
})

The session object here is an autobahn.js websockets connection to a python server (using soundfile . I tried a number of arguments in the place that was labelled by XXXXX in the code.这里的session对象是一个到 python 服务器的autobahn.js websockets 连接(使用soundfile 。我在代码中由XXXXX标记的地方尝试了许多参数。

  1. Just pass the audioBlob .只需通过audioBlob In that case, the python side just receives an empty dictionary.在这种情况下,python 端只会收到一个空字典。
  2. Pass audioBlob.text() in that case, I get something that looks somewhat binary (starts with OggS), but it can't be decoded.在这种情况下,通过audioBlob.text() ,我得到的东西看起来有点二进制(以 OggS 开头),但无法解码。
  3. Pass audioBlob.arrayBuffer() .通过audioBlob.arrayBuffer() In that case the python side receives an empty dictionary.在这种情况下,python 端会收到一个空字典。

A possible solution could be to convert the data to WAV on the serverside (just changing the mime-type on the blob doesn't work) or to find a way to interpret the .text() output on the server side.一个可能的解决方案可能是在服务器端将数据转换为 WAV(仅更改 blob 上的 mime-type 不起作用)或找到一种方法来解释服务器端的.text()输出。

解决方案是使用recorder.js ,然后使用那里的getBuffer方法将波形数据作为Float32Array

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM