首页 > 解决方案 > 从异步任务返回

问题描述

有人可以帮帮我吗?如何将字符串 (voiceInput) 传递回主函数?

using System;
using System.Threading.Tasks;
using Microsoft.CognitiveServices.Speech;

namespace helloworld
{
    class Program
    {
        public static async Task<string> RecognizeSpeechAsync()
        {
            var config = SpeechConfig.FromSubscription("hidden", "westeurope");

            string voiceInput ="name"; 
            // Creates a speech recognizer.
            using (var recognizer = new SpeechRecognizer(config))
            {
                Console.WriteLine("Say something...");

                var result = await recognizer.RecognizeOnceAsync();

                // Checks result.
                if (result.Reason == ResultReason.RecognizedSpeech)
                {
                    voiceInput = result.Text;
                }
                else if (result.Reason == ResultReason.NoMatch)
                {
                    voiceInput = "Sorry, i did not understand you";
                }
                else if (result.Reason == ResultReason.Canceled)
                {
                    var cancellation = CancellationDetails.FromResult(result);
                    Console.WriteLine($"CANCELED: Reason={cancellation.Reason}");

                    if (cancellation.Reason == CancellationReason.Error)
                    {
                        Console.WriteLine($"CANCELED: ErrorCode={cancellation.ErrorCode}");
                        Console.WriteLine($"CANCELED: ErrorDetails={cancellation.ErrorDetails}");
                        Console.WriteLine($"CANCELED: Did you update the subscription info?");
                    }
                    voiceInput = "ERROR";
                }
            }
            Console.WriteLine(voiceInput);
            return voiceInput;
        }

        public static async Task SynthesisToSpeakerAsync(string output)
        {
            // Creates an instance of a speech config with specified subscription key and service region.
            // Replace with your own subscription key and service region (e.g., "westus").
            var config = SpeechConfig.FromSubscription("hidden", "westeurope");

            // Creates a speech synthesizer using the default speaker as audio output.
            using (var synthesizer = new SpeechSynthesizer(config))
            {
                // Receive a text from console input and synthesize it to speaker.
                string text = output;

                using (var result = await synthesizer.SpeakTextAsync(text))
                {
                    if (result.Reason == ResultReason.SynthesizingAudioCompleted)
                    {
                        Console.WriteLine($"Speech synthesized to speaker for text [{text}]");
                    }
                    else if (result.Reason == ResultReason.Canceled)
                    {
                        var cancellation = SpeechSynthesisCancellationDetails.FromResult(result);
                        Console.WriteLine($"CANCELED: Reason={cancellation.Reason}");

                        if (cancellation.Reason == CancellationReason.Error)
                        {
                            Console.WriteLine($"CANCELED: ErrorCode={cancellation.ErrorCode}");
                            Console.WriteLine($"CANCELED: ErrorDetails=[{cancellation.ErrorDetails}]");
                            Console.WriteLine($"CANCELED: Did you update the subscription info?");
                        }
                    }
                }
            }
        }

        static void  Main()
        {
            string output;
            string input;

            output = "Hello, what is your Name?";
            SynthesisToSpeakerAsync(output).Wait();

            input = RecognizeSpeechAsync().Wait();

            output = ($"Hello {input}");
            SynthesisToSpeakerAsync(output).Wait();

            Console.WriteLine("Please press <Return> to continue.");
            Console.ReadLine();
        }
    }
}

这是问题所在: input = RecognizeSpeechAsync().Wait(); 错误:无法将类型“void”隐式转换为“string”

我想将 voiceInput 中的字符串存储到输入中

标签: c#asynchronoustaskspeech-recognition

解决方案


调用.Wait()不会返回结果,它只是等待任务。(并且不一定是最好的方法。)制作你的主要方法asyncawait结果:

static async Task Main()

并在方法内:

await SynthesisToSpeakerAsync(output);

input = await RecognizeSpeechAsync();

output = ($"Hello {input}");
await SynthesisToSpeakerAsync(output);

此外,目前您的方法只返回一个Task

public static async Task RecognizeSpeechAsync()

这使它可以等待,但不返回任何值。要返回一个值,请使用泛型Task<T>

public static async Task<string> RecognizeSpeechAsync()

推荐阅读