【问题标题】:How to transform an audioblob in wav file using Reactjs or Javascript?如何使用 Reactjs 或 Javascript 转换 wav 文件中的音频块?
【发布时间】:2019-10-18 13:34:59
【问题描述】:

我正在使用 Reactjs 前端开发 VUI 界面。我有一个可以播放的 BLOB 文件,但我想使用 REACT 或 Javascript 将其转换为 .WAV 文件以将其发送到我的服务器。

我尝试了很多东西,但没有找到解决办法

toggleRecording() {
    if (this.state.start === 1) {
      console.log("we start recording", this.state.start)
      this.setState({ start: 0, recognition: "" })

      const constraints = {
        audio: {
          sampleRate: 16000,
          channelCount: 1,
        }
      }

      navigator.mediaDevices.getUserMedia(constraints)
        .then(stream => {
          console.log(this);
          this.recorder = new MediaRecorder(stream);
          this.recorder.start();

          const audioChunks = [];
          this.recorder.addEventListener("dataavailable", event => {
            audioChunks.push(event.data);
          });

          this.recorder.addEventListener("stop", () => {
            const audioBlob = new Blob(audioChunks, { 'type': 'audio/wav' });

            const audioUrl = URL.createObjectURL(audioBlob);

            console.log("test: ", audioUrl)
            console.log(audioBlob.type)

            fetch('http://127.0.0.1:6060/api/sendaudio', {
              method: "post",
              headers: { 'Content-Type': 'audio/wav' },
              body: audioBlob
            })
              .then(response => {
                return response.text()
              }).then(text => {
                console.log(text);
                this.setState({ recognition: text })
              });

            //to play the audio file:
            const audio = new Audio(audioUrl);
            audio.play();
          });
        });
    }

我希望获得一个 Wav 文件以发布到我的服务器,但不知道该怎么做....

【问题讨论】:

标签: javascript reactjs voice-recognition voice mediarecorder


【解决方案1】:

如果添加新依赖没有问题,可以试试这个包:https://www.npmjs.com/package/audiobuffer-to-wav

希望对你有用

【讨论】:

    猜你喜欢
    • 2014-09-12
    • 2019-01-07
    • 2011-12-07
    • 1970-01-01
    • 2021-08-15
    • 1970-01-01
    • 2018-01-23
    • 1970-01-01
    • 2019-12-31
    相关资源
    最近更新 更多