如何在c#中使用谷歌语音识别api?

时间:2014-08-05 21:10:11

标签: c# speech-recognition

我想从c#获取音频文件并发送到谷歌语音识别API,以获得“语音到文本”的答案。

我的代码是这样的:

try
{                
    byte[] BA_AudioFile = GetFile(filename);              
    HttpWebRequest _HWR_SpeechToText = null;
    _HWR_SpeechToText =
                (HttpWebRequest)HttpWebRequest.Create(
                    "https://www.google.com/speech-api/v2/recognize?output=json&lang=" + DEFAULT_LANGUAGE + "&key=" + key);
    _HWR_SpeechToText.Credentials = CredentialCache.DefaultCredentials;
    _HWR_SpeechToText.Method = "POST";
    _HWR_SpeechToText.ContentType = "audio/x-flac; rate=44100";
    _HWR_SpeechToText.ContentLength = BA_AudioFile.Length;
    Stream stream = _HWR_SpeechToText.GetRequestStream();
    stream.Write(BA_AudioFile, 0, BA_AudioFile.Length);
    stream.Close();

    HttpWebResponse HWR_Response = (HttpWebResponse)_HWR_SpeechToText.GetResponse();
    if (HWR_Response.StatusCode == HttpStatusCode.OK)
    {
        StreamReader SR_Response = new StreamReader(HWR_Response.GetResponseStream());
        Console.WriteLine(SR_Response.ToString());
    }

}
catch (Exception ex)
{
    Console.WriteLine(ex.ToString());
}

此部分用于上传file.wav并获取我从互联网上找到的Google API的响应。

但是我的代码总是捕获例外:

在调用_HWR_SpeechToText.GetResponse()之前,必须将内容长度字节写入请求流。但我已经写过ContextLength了。

所以我的问题是为什么我的程序失败了?这是因为谷歌链接或我不恰当使用的HTTPWebRequest?

这是我获得API密钥的正确位置吗?

enter image description here

1 个答案:

答案 0 :(得分:9)

我自己测试过,如果你有一个有效的API密钥,下面是一个可行的解决方案。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Net;
    using System.IO;

    namespace GoogleRequest
    {
     class Program
     {
        static void Main(string[] args)
        {
            try
            {

                FileStream fileStream = File.OpenRead("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();
                HttpWebRequest _HWR_SpeechToText = null;
                _HWR_SpeechToText =
                            (HttpWebRequest)HttpWebRequest.Create(
                                "https://www.google.com/speech-api/v2/recognize?output=json&lang=en-us&key=YOUR_API_KEY_HERE");
                _HWR_SpeechToText.Credentials = CredentialCache.DefaultCredentials;
                _HWR_SpeechToText.Method = "POST";
                _HWR_SpeechToText.ContentType = "audio/x-flac; rate=44100";
                _HWR_SpeechToText.ContentLength = BA_AudioFile.Length;
                Stream stream = _HWR_SpeechToText.GetRequestStream();
                stream.Write(BA_AudioFile, 0, BA_AudioFile.Length);
                stream.Close();

                HttpWebResponse HWR_Response = (HttpWebResponse)_HWR_SpeechToText.GetResponse();
                if (HWR_Response.StatusCode == HttpStatusCode.OK)
                {
                    StreamReader SR_Response = new StreamReader(HWR_Response.GetResponseStream());
                    Console.WriteLine(SR_Response.ReadToEnd());
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            Console.ReadLine();
        }
    }
}