简体   繁体   中英

C# Google Speech api v2 returning empty string

I had the google speech v2 API working perfectly fine about a week ago and it was returning results with no problems however testing it today with the same .flac file keeps returning "{\\"result\\":[]}" no matter what I try. Wondering if A) Anyone else is having this problem or B) Anyone has a solution to my problem my code is below thanks!

        public static String gvoice ()
    {
        //set the input file name
        FileStream fileStream = File.OpenRead(@"test1.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;

        //this points to the google speech API (key goes at end after &key=)
        _HWR_SpeechToText =
        (HttpWebRequest)HttpWebRequest.Create(
        "https://www.google.com/speech-api/v2/recognize?output=json&lang=en-us&key=" + key);

        _HWR_SpeechToText.Credentials = CredentialCache.DefaultCredentials;
        _HWR_SpeechToText.Method = "POST";
        //sets kMhz and file type (flac)
        _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());
            string result = SR_Response.ReadToEnd();
            return result;
        }
        else
        {
            return "error";
        }


    }

The requirements for the sound file have changed. It should be a MONO Flac file at 16000 rate

A) Anyone else is having this problem

Sure, Google impose usage limits on the API which makes it less practical than you might think.

or B) Anyone has a solution to my problem my code is below thanks!

Use other API's, there are many of them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM