简体   繁体   English

如何在 JavaScript 中录制麦克风音频并提交给 DialogFlow?

[英]How to record microphone audio in JavaScript and submit to DialogFlow?

如何在 JavaScript 中录制来自麦克风的音频并将其提交给 DialogFlow,而无需通过服务器?

There are two parts to this question:这个问题有两个部分:

  1. How to record microphone audio in a format DialogFlow will understand.如何以 DialogFlow 理解的格式录制麦克风音频。
  2. How to actually submit that audio to DialogFlow, with proper authentication.如何通过适当的身份验证将该音频实际提交给 DialogFlow。

Part 1第1部分

For recording microphone audio in a format DialogFlow will understand, I use opus-recorder , then convert the blob it returns using the code below:为了以 DialogFlow 能够理解的格式录制麦克风音频,我使用opus-recorder ,然后使用以下代码转换它返回的 blob:

function BlobToDataURL(blob: Blob) {
    return new Promise((resolve, reject)=>{
        const reader = new FileReader();
        reader.addEventListener("loadend", e=>resolve(reader.result as string));
        reader.readAsDataURL(blob);
    }) as Promise<string>;
}

const micRecorder = new Recorder({
    encoderSampleRate: 16000,
    originalSampleRateOverride: 16000, // necessary due to Google bug? (https://github.com/chris-rudmin/opus-recorder/issues/191#issuecomment-509426093)
    encoderPath: PATH_TO_ENCODER_WORKER_JS,
});
micRecorder.ondataavailable = async typedArray=>{
    const audioData = new Blob([typedArray], {type: "audio/ogg"});
    const audioData_dataURL = await BlobToDataURL(audioData);
    const audioData_str = audioData_dataURL.replace(/^data:.+?base64,/, "");

    // here is where you need part 2, to actually submit the audio to DialogFlow
};
micRecorder.start();

Part 2第2部分

To submit the audio-data to DialogFlow, see my answer here: https://stackoverflow.com/a/57857698/2441655要将音频数据提交给 DialogFlow,请在此处查看我的回答: https ://stackoverflow.com/a/57857698/2441655

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

相关问题 如何使用 RecordRTC 录制屏幕+音频+麦克风 - How to record screen+audio+microphone with RecordRTC 任何人都可以建议如何使用html5和javascript在网站上录制麦克风的音频 - Can anyone suggest how to record audio from microphone on a website using html5 and javascript 如何记录网络/浏览器音频输出(不是麦克风音频) - How to record web/browser audio output (not microphone audio) 如何通过不使用内置麦克风在移动设备上的应用程序中录制音频 - How to record audio within a application on a mobile device, by not using the built in microphone Javascript:麦克风音频下载链接? - Javascript: Microphone audio to download link? 使用带有HTML5的用户麦克风录制音频 - Record Audio Using the Users Microphone with HTML5 分析来自麦克风Javascript的音频输入 - Analyze audio input from microphone Javascript Javascript - 将录制的音频流式传输为麦克风输入 - Javascript - Streaming a recorded audio as microphone input (Javascript) 来自媒体流的麦克风和音频不同步 - (Javascript) Microphone and audio from mediastream are out of sync 如何在javascript中使用audioWorklet和AudioWorkletProcessor录制音频? - How to record audio using audioWorklet and AudioWorkletProcessor in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM