首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从异步任务返回

从异步任务返回
EN

Stack Overflow用户
提问于 2019-10-01 12:49:52
回答 2查看 232关注 0票数 2

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

代码语言:javascript
复制
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();Error:不能隐式地将类型'void‘转换为'string’

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-10-01 12:54:12

调用.Wait()不会返回结果,它只是等待一个任务。(也不一定是最好的方法。)让您的主要方法asyncawait获得结果:

代码语言:javascript
复制
static async Task Main()

在该方法范围内:

代码语言:javascript
复制
await SynthesisToSpeakerAsync(output);

input = await RecognizeSpeechAsync();

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

加上,目前您的方法只返回一个Task

代码语言:javascript
复制
public static async Task RecognizeSpeechAsync()

这使得它是不可接受的,但不返回任何值。若要返回值,请使用泛型Task<T>

代码语言:javascript
复制
public static async Task<string> RecognizeSpeechAsync()
票数 3
EN

Stack Overflow用户

发布于 2019-10-01 12:53:48

Wait()是一种空法。它什么都不回。在任何情况下,将主要方法更改为static async Task Main()并使用await:

代码语言:javascript
复制
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();
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58185127

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档