Google语音客户端api中的结果为空

时间:2018-10-11 23:36:13

标签: c# angular google-speech-api

我正在尝试使用Google语音API实现语音识别。

我在网页上录制了原始音频,然后将原始文件发送到使用Google语音API尝试识别的服务器。

这是我的角度代码

start() {
navigator.mediaDevices.getUserMedia({ audio: true })
  .then(stream => {
    this.mediaRecorder = new MediaRecorder(stream);
    this.mediaRecorder.start();

    const audioChunks = [];

    this.mediaRecorder.addEventListener("dataavailable", event => {
      audioChunks.push(event.data);
    });

    this.mediaRecorder.addEventListener("stop", () => {
      const audioBlob = new Blob(audioChunks, { type: "audio/wav"});
      const audioUrl = URL.createObjectURL(audioBlob);

      this.fileService.saveFile(audioBlob).subscribe(res => { console.log(res); });

      const audio = new Audio(audioUrl);
      audio.play();
    });
  });
}

这是我的C#控制器代码'

    private string GetSpeechToText()
    {
        string result = string.Empty;

        try
        {
            SpeechClient speechClient = SpeechClient.Create();

            RecognitionConfig config = new RecognitionConfig()
            {
                Encoding = RecognitionConfig.Types.AudioEncoding.Linear16,
                SampleRateHertz = 16000,
                LanguageCode = "en-US",
            };

            RecognizeResponse response = speechClient.Recognize(config,
                RecognitionAudio.FromFile(fileName));

            foreach (var responseResult in response.Results)
            {
                foreach (var alternative in responseResult.Alternatives)
                {
                    result += alternative.Transcript;
                }
            }
        }
        catch (System.Exception ex)
        {
            Console.WriteLine(ex.Message);
            return result;
        }

        return result;
    }

写入文件后,音频在客户端和服务器端的文件上都可以正常播放。

我正确包含了GOOGLE_APPLICATION_CREDENTIALS路径。

但是我得到的回应是空洞的。请告诉我我是否想念东西

0 个答案:

没有答案