Google Speech Api 2 Error 400 Bad Request

I am trying to convert voice to text using the Google Speech API. I have some sample code and it works with the default file (good-morning-google.flac). But when I change the file with my voice, I get an Error 400 error request. I think the problem occurs when changing the format file on this line

 _HWR_SpeechToText.ContentType = "audio/x-pcm;bit=16;rate=16000";  //"audio/x-wav";// "audio/x-flac; rate=44100";

      

my code

  private void button1_Click(object sender, EventArgs e)
    {
         try
    {

        FileStream fileStream = File.OpenRead("c:\\Test0001.wav");// ("c:\\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=AIzaSyAS3klnddmHrBJS8ZtvIe4pnfRK-qPnF0U");
        _HWR_SpeechToText.Credentials = CredentialCache.DefaultCredentials;
        _HWR_SpeechToText.Method = "POST";
        _HWR_SpeechToText.ContentType = "audio/x-pcm;bit=16;rate=16000";  //"audio/x-wav";// "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());
            MessageBox.Show(SR_Response.ReadToEnd());

            Console.WriteLine(SR_Response.ReadToEnd());
        }

    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());

        Console.WriteLine(ex.ToString());
    }

    Console.ReadLine();
}
    }

      

can anyone help me to solve this problem. Thank.

+3


source to share





All Articles