我有点失落,我真的希望你能帮助我。我正在尝试使用Google Speech API。我的目标是获取一个音频文件,将其发送到Speech API并让它返回文本。这是我到目前为止所做的: •在Google上创建了一个项目 •创建了一个服务帐户 •注册了一个环境变量,指向我从Google下载的json文件。 •将NuGet GoogleSpeechAPI安装到Visual Studio 2015中
我真的不知道从哪里开始。这是我到目前为止尝试实现的内容:(从codeproject上的借用代码修改)
var service = new CloudSpeechAPIService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Ayaz",
});
//FileStream fileStream = File.OpenRead(@"C:\OA\VS_Projects\Speech\good-morning-google.flac");
//MemoryStream memoryStream = new MemoryStream();
//memoryStream.SetLength(fileStream.Length);
//fileStream.Read(memoryStream.GetBuffer(), 0, (int)fileStream.Length);
//byte[] BA_AudioFile = memoryStream.GetBuffer();
string path = @"C:\OA\VS_Projects\Speech\good-morning-google.flac";
byte[] BA_AudioFile = System.IO.File.ReadAllBytes(path);
SyncRecognizeRequest mySyncReq = new SyncRecognizeRequest();
AsyncRecognizeRequest myRequest = new AsyncRecognizeRequest();
RecognitionAudio audio = new RecognitionAudio();
RecognitionConfig rConfig = new RecognitionConfig();
audio.Content = System.Convert.ToBase64String(BA_AudioFile);
rConfig.Encoding = "FLAC"; // LINEAR16 FLAC MULAW AMR AMR_WB
rConfig.LanguageCode = "us-EN";
rConfig.SampleRate = 44100;
myRequest.Audio = audio;
myRequest.Config = rConfig;
mySyncReq.Audio = audio;
mySyncReq.Config = rConfig;
//service.Speech.Asyncrecognize(myRequest);
Operation iOp;
//Operation status;
//Syncrecognize Asyncrecognize
iOp = service.Speech.Asyncrecognize(myRequest).Execute();
while(true)
{
if (iOp.Done == true)
{
break;
}
}