title: Text to Speech using Google Translator in Java
body: <p>I have written a very simple program to convert Text, inputted from a file named 'writer.txt' in my C:\ drive to speech using Google Translator. Everything is working fine, I am getting the correct output in a mp3 file named 'output.mp3'. But, my code is showing 1 error which I am not able to Debug. Please can anyone help me on this...</p>
  
  <p>Here's my code.</p>
  
  <pre><code>package tts;
  import java.io.BufferedInputStream;
  import java.io.BufferedOutputStream;
  import java.io.ByteArrayOutputStream;
  import java.io.File;
  import java.io.FileOutputStream;
  import java.io.BufferedReader;
  import java.io.FileReader;
  import java.io.IOException;
  import java.net.HttpURLConnection;
  import java.net.URL;
  import java.net.URLEncoder;
  
  class TTS {
      private static final String TEXT_TO_SPEECH_SERVICE = 
              "http://translate.google.com/translate_tts";
      private static final String USER_AGENT =  
              "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:11.0) " +
              "Gecko/20100101 Firefox/11.0";
  
      public static void main(String[] args) throws Exception {
          if (args.length != 1) {
              System.out.println("Usage: SimpleTextToSpeech &lt;language&gt; &lt;text&gt; " +
                      "where: ");
              System.out.println();
              System.out.println("- Language: all languages accepted by " +
                      "google translate, in this example, we'll use fr, en");
              System.out.println("- Text : If text is more than one word, " +
                      "then is must be put inside double quote, for example:");
              System.out.println("\tjava SimpleTextToSpeech en Hello");
              System.out.println("\tjava SimpleTextToSpeech en \"Hello World\"");
              System.exit(1);
          }
          BufferedReader br=new BufferedReader(new FileReader("C:\\writer.txt"));
          File output = new File("output.mp3");
          BufferedOutputStream out = 
                  new BufferedOutputStream(new FileOutputStream(output));
  
          Language language = Language.valueOf(args[0]);//.toUpperCase());
          String text;
          text=br.readLine();
          while(text!=null)
          {
              text = URLEncoder.encode(text, "utf-8");
              new TTS().go(language, text,output,out);
              text=br.readLine();
  
          }  
          out.close();
      }
  
      public void go(Language language, String text, File output,BufferedOutputStream out) throws Exception {
          // Create url based on input params
          if(text!=null)
          {
          String strUrl = TEXT_TO_SPEECH_SERVICE + "?" + 
                  "tl=" + language + "&amp;q=" + text;
          URL url = new URL(strUrl);
  
          // Etablish connection
          HttpURLConnection connection = (HttpURLConnection) url.openConnection();
          // Get method
          connection.setRequestMethod("GET");
          // Set User-Agent to "mimic" the behavior of a web browser. In this 
          // example, I used my browser's info
          connection.addRequestProperty("User-Agent", USER_AGENT);
          connection.connect();
  
          // Get content
          BufferedInputStream bufIn = 
                  new BufferedInputStream(connection.getInputStream());
          byte[] buffer = new byte[1024];
          int n;
          ByteArrayOutputStream bufOut = new ByteArrayOutputStream();
          while ((n = bufIn.read(buffer)) &gt; 0) {
              bufOut.write(buffer, 0, n);
          }
  
          // Done, save data;
          out.write(bufOut.toByteArray());
          out.flush();
          System.out.println("Done");
          }
          //else
           // out.close();
      }
  
      public enum Language {
          fr("french"),
          en("english"),
          ca("Croatian"),
          de("german");
  
          private final String language;
          private Language(String language) {
              this.language = language;
          }
  
          public String getFullName() {
              return language;
          }
      }
  }
  </code></pre>
  
  <p>Thanks in advance.  </p>