Search code examples
c#async-awaitanonymous-functiongoogle-speech-api

Make some changes of google streaming speech recognition and it does not work


I'm working on the google streaming speech recognition.Now I'm trying to take the parameters out of the WriteAsync, but I can't figure out why I can't receive any text. Here's the previous code:

 await streamingCall.WriteAsync(
      new StreamingRecognizeRequest()
      {
          StreamingConfig = new StreamingRecognitionConfig()
          {
              Config = new RecognitionConfig()
              {
                Encoding = RecognitionConfig.Types.AudioEncoding.Linear16,
                SampleRateHertz = 32000,
                LanguageCode = "en",
               },
                InterimResults = true,
            }
       });

Here's the code that I made some changes:

RecognitionConfig config = new RecognitionConfig();
config.Encoding = RecognitionConfig.Types.AudioEncoding.Linear16;
config.SampleRateHertz = 32000;
config.LanguageCode = "en";
await streamingCall.WriteAsync(
  new StreamingRecognizeRequest()
   {
      StreamingConfig = new StreamingRecognitionConfig()
       {
         InterimResults = true,
        }
    });

Solution

  • You are not using your RecognitionConfig:

    RecognitionConfig config = new RecognitionConfig();
    config.Encoding = RecognitionConfig.Types.AudioEncoding.Linear16;
    config.SampleRateHertz = 32000;
    config.LanguageCode = "en";
    await streamingCall.WriteAsync(
        new StreamingRecognizeRequest()
        {
            StreamingConfig = new StreamingRecognitionConfig()
            {
                Config = config, // You are missing this line
                InterimResults = true,
            }
        });