簡體   English   中英

從異步任務返回

[英]return from async task

有人可以幫幫我嗎? 如何將字符串 (voiceInput) 傳遞回主 function?

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 中的字符串存儲到輸入中

調用.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()

Wait()是一個無效方法。 它不返回任何東西。 在任何情況下,將 Main 方法更改為static async Task Main()並使用 await:

static void  Main()
{
    var output = "Hello, what is your Name?";
    await SynthesisToSpeakerAsync(output);

    var input = await RecognizeSpeechAsync();

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM