title: Google speech to text API in C#
body: <p>My program get a correct respon from google when the flac file recorded manual by using windows's sound recorder and convert it using a software converter.<br />
  But when I use the file that recorded by my program, I got "{"result":[]}
  " from google. What should I do?
  here is my code : <br />
  the sender :<br /></p>
  
  <pre><code>    private static void CopyStream(FileStream fileStream, Stream requestStream)
      {
          var buffer = new byte[32768];
          int read;
          while ((read = fileStream.Read(buffer, 0, buffer.Length)) &gt; 0)
          {
              requestStream.Write(buffer, 0, read);
          }
      }
  
      private static void ConfigureRequest(HttpWebRequest request)
      {
          request.KeepAlive = true;
          request.SendChunked = true;
          request.ContentType = "audio/x-flac; rate=44100";
          request.UserAgent =
              "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
          request.Headers.Set(HttpRequestHeader.AcceptEncoding, "gzip,deflate,sdch");
          request.Headers.Set(HttpRequestHeader.AcceptLanguage, "en-GB,en-US;q=0.8,en;q=0.6");
          request.Headers.Set(HttpRequestHeader.AcceptCharset, "ISO-8859-1,utf-8;q=0.7,*;q=0.3");
          request.Method = "POST";
      }
      using (var fileStream = new FileStream(@"C:\Users\Ahmad Mustofa\Documents\Visual Studio 2010\Projects\FP\FP\bin\Debug\voice.flac", FileMode.Open))
      {
          const string requestUrl = "https://www.google.com/speech-api/v2/recognize?output=json&amp;lang=ar-sa&amp;key=AIzaSyBJ6VJ326Rpb23msih2wGhXENEwU1TF1PA&amp;client=chromium&amp;maxresults=1&amp;pfilter=2";
          var request = (HttpWebRequest)WebRequest.Create(requestUrl);
          ConfigureRequest(request);
          var requestStream = request.GetRequestStream();
          CopyStream(fileStream, requestStream);
  
          using (var response = request.GetResponse())
          {
              using (var responseStream = response.GetResponseStream())
              {
                  using (var zippedStream = new GZipStream(responseStream, CompressionMode.Decompress))
                  {
                       using (var sr = new StreamReader(zippedStream))
                       {
                            var res = sr.ReadToEnd();
                            state.Text = res;
                       }
                  }
              }
          }
      }
  </code></pre>
  
  <p>the wav recorder: <br /></p>
  
  <pre><code>        private void sourceStream_DataAvailable(object sender, NAudio.Wave.WaveInEventArgs e)
          {
               if (waveWriter == null) return;
  
               waveWriter.WriteData(e.Buffer, 0, e.BytesRecorded);
               waveWriter.Flush();
          }
          fileName = "C:\\Users\\Ahmad Mustofa\\Documents\\Visual Studio 2010\\Projects\\FP\\FP\\bin\\debug\\voice.wav";
          int deviceNumber = hardware.SelectedItems[0].Index;
          try
          {
              sourceStream = new NAudio.Wave.WaveIn();
              sourceStream.DeviceNumber = deviceNumber;
              sourceStream.WaveFormat = new NAudio.Wave.WaveFormat(44100, NAudio.Wave.WaveIn.GetCapabilities(deviceNumber).Channels);
  
              sourceStream.DataAvailable += new EventHandler&lt;NAudio.Wave.WaveInEventArgs&gt;(sourceStream_DataAvailable);
              waveWriter = new NAudio.Wave.WaveFileWriter(fileName, sourceStream.WaveFormat);
  
              sourceStream.StartRecording();
          }
          catch (Exception ex)
          {
              state.Text = "disini" + ex.Message;
          }
  </code></pre>
  
  <p>flac converter: <br /></p>
  
  <pre><code>        string inputFile = Path.Combine("wav ", input);
          string outputFile = Path.Combine("flac", Path.ChangeExtension(input, ".flac"));
  
          if (!File.Exists(inputFile))
              throw new ApplicationException("Input file " + inputFile + " cannot be found!");
  
          WavReader wav = new WavReader(inputFile);
  
          using (var flacStream = File.Create(outputFile))
          {
              FlacWriter flac = new FlacWriter(flacStream, wav.BitDepth, wav.Channels, wav.SampleRate);
              // Buffer for 1 second's worth of audio data
              byte[] buffer = new byte[wav.Bitrate / 8];
              int bytesRead;
              do
              {
                  bytesRead = wav.InputStream.Read(buffer, 0, buffer.Length);
                  flac.Convert(buffer, 0, bytesRead);
              } while (bytesRead &gt; 0);
              flac.Dispose();
              flac = null;
          }
  </code></pre>