有人可以帮我吗?如何将字符串(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();
}
}
}
这是问题所在: 输入= RecognizeSpeechAsync()。Wait(); 错误:无法将类型'void'隐式转换为'string'
我想将语音输入中的字符串存储到输入中
答案 0 :(得分:3)
调用.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()
答案 1 :(得分:0)
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();
}