有人能帮帮我吗?如何将字符串(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();Error:不能隐式地将类型'void‘转换为'string’
我想将字符串从voiceInput存储到输入中
发布于 2019-10-01 12:54:12
调用.Wait()不会返回结果,它只是等待一个任务。(也不一定是最好的方法。)让您的主要方法async和await获得结果:
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()发布于 2019-10-01 12:53:48
Wait()是一种空法。它什么都不回。在任何情况下,将主要方法更改为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();
}https://stackoverflow.com/questions/58185127
复制相似问题