记录来自html页面的语音,然后使用JS将其转换为文本?

时间:2019-02-21 13:34:16

标签: javascript speech-recognition speech-to-text

下面的代码只能录制声音并提供u按钮以下载它,以帮助录制声音,然后将其转换为文本。

<script>
    navigator.mediaDevices.getUserMedia({audio:true})
        .then(stream => {
            rec = new MediaRecorder(stream);
            rec.ondataavailable = e => {
                audioChunks.push(e.data);
                if (rec.state == "inactive"){
            let blob = new Blob(audioChunks,{type:'audio/x-mpeg-3'});
            recordedAudio.src = URL.createObjectURL(blob);
            recordedAudio.controls=true;
            recordedAudio.autoplay=true;
            audioDownload.href = recordedAudio.src;
            audioDownload.download = 'mp3';
            audioDownload.innerHTML = 'download';
         }
            }
        })
        .catch(e=>console.log(e));

    startRecord.onclick = e => {
      startRecord.disabled = true;
      stopRecord.disabled=false;
      audioChunks = [];
      rec.start();
    }
    stopRecord.onclick = e => {
      startRecord.disabled = false;
      stopRecord.disabled=true;
      rec.stop();
 }
</script>       

0 个答案:

没有答案