我正在使用Google Speech API for C#,并使用LongRunningRecognize来检索结果,如下所示:
var speech = SpeechClient.Create();
var longOperation = speech.LongRunningRecognize(new RecognitionConfig()
{
Encoding = GlobalVariables.Encoding,
SampleRateHertz = GlobalVariables.SampleRateHz,
LanguageCode = GlobalVariables.Language,
}, RecognitionAudio.FromStorageUri("gs://bucketname/" + TargetFileName));
longOperation = longOperation.PollUntilCompleted();
var response = longOperation.Result;
StringBuilder tw = new StringBuilder();
foreach (var result in response.Results)
{
foreach (var alternative in result.Alternatives)
{
Console.WriteLine($"Transcript: { alternative.Transcript}");
tw.AppendLine(alternative.Transcript);
}
}
TranslatedText = tw.ToString();
那很好,我得到了结果-但是:我正在使用很大的文件(大约20-30分钟的音频文件),并且我希望看到中间结果。所以我发现我可以像这样以某种方式使用异步方法:
Task analyze = Task.Run(async () =>
{
var speech = SpeechClient.Create();
Task<Operation<LongRunningRecognizeResponse, LongRunningRecognizeMetadata>> longOperation = speech.LongRunningRecognizeAsync(new RecognitionConfig()
{
Encoding = GlobalVariables.Encoding,
SampleRateHertz = GlobalVariables.SampleRateHz,
LanguageCode = GlobalVariables.Language,
}, RecognitionAudio.FromStorageUri("gs://bucketname/" + TargetFileName));
while (!longOperation.IsCompleted)
{
if(longOperation.Result != null && longOperation.Result.Metadata != null)
{
LongRunningRecognizeMetadata meta = longOperation.Result.Metadata;
ProgressValue = meta.ProgressPercent;
//Add results to variable "TranslatedText" here -how?
}
}
});
await analyze;
虽然我无法使它正常工作,并且在这种情况下对语音API的记录也不够好,至少我还没有发现任何可以解决这种情况的东西。
有人可以帮我吗?
谢谢!