代码之家  ›  专栏  ›  技术社区  ›  Jeahel

使用Web Audio API录制的音频在开始时被截断

  •  1
  • Jeahel  · 技术社区  · 7 年前

    在这个过程中,用户必须说出写在屏幕上的单词,我会分别记录他们说出的每个单词。我的问题是,一些录音在开头或结尾被截断,只留下一部分单词。

    以下是我请求访问麦克风的简单代码:

    navigator.getUserMedia({
      audio: {
        echoCancellation: false,
        noiseSuppression: false,
        autoGainControl: false
      }
    }, this._handleUserMedia.bind(this), () => {
      this.setState({
        recorderAvailable: false
      });
    });
    
    _handleUserMedia(stream) {
      this._mediaRecorder = new MediaRecorder(stream);
    
      this._mediaRecorder.ondataavailable = event => {
        this._mediaBuffer.push(event.data);
      };
    
      this._mediaRecorder.onstart = () => {
        console.log("Recording started");
        document.getElementById("wordsStage").style.color = "red";
      }
    
      this._mediaRecorder.onstop = () => {
        // Create an URL to download the recording
        this.state.results[this.state.currentWordIdx].recordingUrl = URL.createObjectURL(new Blob(this._mediaBuffer));
    
        // Reinitialize the buffer for next recording
        this._mediaBuffer = [];
        console.log("Recording stopped");
        document.getElementById("wordsStage").style.color = "black";
    
        this._gotoNextWord();
      };
    
      this._gotoNextWord();
    }
    

    这是我开始/停止录制的代码:

    _registerAnswer(isCorrect) {
      this.state.results.push({
        word: this.props.data.words[this._currentWordIdx],
        isCorrect
      });
    
      this._mediaRecorder && this._mediaRecorder.state === "recording" && this._mediaRecorder.stop();
    
      // mediaRecorder.onstop handler will trigger the next word once the recording has been saved
    }
    
    _gotoNextWord() {
      const dataSource = this.state.isDoingExamples ? this.props.data.examples : this.props.data.words;
      if (this._currentWordIdx < dataSource.length - 1) {
        // Starts the recording
        !this.state.isDoingExamples && this._mediaRecorder && this._mediaRecorder.start(500);
    
        // displays the concentration point
        this._displayWordOnStage(null, true);
    
        // displays the current word after the concentration's point duration elapsed
        setTimeout(() => this._displayWordOnStage(++this._currentWordIdx), Shared.FocusPointDurationInMs);
      } else if (this.state.isDoingExamples) {
        this._currentWordIdx = -1;
        this.setState({
            isDoingExamples: false
          },
          this._gotoNextWord
        );
      } else {
        this._finishTraining();
      }
    }
    

    here

    编辑:

    this blog post 表示在Chrome中 noiseSuppression 虽然约束处于启用状态,但不受支持,因此无法将其禁用。较新版本的Chrome确实有此限制,但嵌入Electron的版本没有此限制:使用时未列出此限制 navigator.mediaDevices.getSupportedConstraints(); . 这可能就是大多数单词在开头被截断的原因吧?
    我试过用电子,但没用反应,没关系,所以不可能是。。。

    编辑3 :我将Electron更新为最新版本(2.0.3),其中包括Chrome 61.0.3163.100。我仍然有这个问题。

    0 回复  |  直到 7 年前