简体   繁体   中英

return from async task

Can someone please help me? how can I pass the string (voiceInput) back to the main 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();
        }
    }
}

Here's is the Problem: input = RecognizeSpeechAsync().Wait(); Error: Cannot implicitly convert type 'void' to 'string'

I'd like to store the string from voiceInput into input

Calling .Wait() doesn't return a result, it just awaits a task. (And isn't necessarily the best way to do it.) Make your main method async and await the result:

static async Task Main()

and within the method:

await SynthesisToSpeakerAsync(output);

input = await RecognizeSpeechAsync();

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

Additionally , currently your method only returns a Task :

public static async Task RecognizeSpeechAsync()

Which makes it awaitable, but returns no value. To return a value, use the generic Task<T> :

public static async Task<string> RecognizeSpeechAsync()

Wait() is a void method. It doesn't return anything. In any case, change the Main method to static async Task Main() and use 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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM