您必须从此处下载示例应用程序:
https ://cloud.google.com/speech/docs/samples
您将找到语音示例:快速入门和识别。
Recogize 有很多选择,其中之一是 Listen。此示例是流式音频,并将结果连续写入控制台。
该示例使用 protobuf 字节流进行流式传输。这是代码的主要部分:
var credential = GoogleCredential.FromFile( "privatekey.json" ).CreateScoped( SpeechClient.DefaultScopes );
var channel = new Grpc.Core.Channel( SpeechClient.DefaultEndpoint.ToString(), credential.ToChannelCredentials() );
var speech = SpeechClient.Create( channel );
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 = "hu",
},
InterimResults = true,
}
} );
当然必须改变语言。
然后必须流式传输内容:
streamingCall.WriteAsync(
new StreamingRecognizeRequest()
{
AudioContent = Google.Protobuf.ByteString
.CopyFrom( args.Buffer, 0, args.BytesRecorded )
} ).Wait();