【发布时间】:2018-10-01 21:20:33
【问题描述】:
我想在 GRPC 上的 C# 上实现流式语音识别,但只有一个 API KEY
这种类型的密钥 --> https://cloud.google.com/docs/authentication/api-keys
我通过在网上搜索知道,在其他语言(android、IO)中,它们可以“拦截”并添加带有如下键的标题:
Metadata.Key.of("x-goog-api-key", "value");
如何在 c# 中制作相同的 BUT?
我的代码,如果出于任何原因需要的话。
public static async Task<object> StreamingRecognizeAsync(Stream audio)
{
var speech = SpeechClient.Create();
var streamingCall = speech.StreamingRecognize();
// Write the initial request with the config.
await streamingCall.WriteAsync(
new StreamingRecognizeRequest()
{
StreamingConfig = new StreamingRecognitionConfig()
{
Config = new RecognitionConfig()
{
Encoding =
RecognitionConfig.Types.AudioEncoding.Linear16,
SampleRateHertz = 16000,
LanguageCode = "en",
},
InterimResults = true,
}
});
// Print responses as they arrive.
Task printResponses = Task.Run(async () =>
{
while (await streamingCall.ResponseStream.MoveNext(
default(CancellationToken)))
{
foreach (var result in streamingCall.ResponseStream
.Current.Results)
{
foreach (var alternative in result.Alternatives)
{
Console.WriteLine(alternative.Transcript);
}
}
}
});
var buffer = new byte[32 * 1024];
int bytesRead;
while ((bytesRead = await audio.ReadAsync(
buffer, 0, buffer.Length)) > 0)
{
await streamingCall.WriteAsync(
new StreamingRecognizeRequest()
{
AudioContent = Google.Protobuf.ByteString
.CopyFrom(buffer, 0, bytesRead),
});
await Task.Delay(500);
};
await streamingCall.WriteCompleteAsync();
await printResponses;
return 0;
}
非常感谢
【问题讨论】:
标签: c# interceptor google-speech-api