如何在C#中使用Google语音识别API? [英] How to use google speech recognition api in c#?

查看:107
本文介绍了如何在C#中使用Google语音识别API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从c#中获取音频文件,并将其发送到Google语音识别API以获取语音转文本答案。

I want to get the audio file from c# and send to google speech recognition API for get the "speech to text" answer.

我的代码是这样的:

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的响应我从Internet上找到的API。

This part is for upload the file.wav and get the response for the google API, which I find from Internet.

但是我的代码总是捕获以下异常:

But my code always catches the exceptions:

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

you must write content length bytes to the request stream before calling at _HWR_SpeechToText.GetResponse(); But I already wroteh the ContextLength.

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

So my question is why my program failed? It's because the google link or the HTTPWebRequest I used inappropriately?

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

Is this the right place I got the API key?

推荐答案

我自己对此进行了测试,如果您具有有效的API密钥,以下是一个可行的解决方案。

Just tested this myself, below is a working solution if you have a valid API key.

    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();
        }
    }
}

这篇关于如何在C#中使用Google语音识别API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆