Question

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...

Here's my 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 <language> <text> " +
                    "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 + "&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)) > 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;
        }
    }
}

Thanks in advance.

Was it helpful?

Solution

First error your code gave was FileNot found in the line

BufferedReader br=new BufferedReader(new FileReader(D:\writer.txt"));

I hope you have the file in your local machine.

Next the file needs to have some data to translate.

Last error I got was connection refused. Which will be solved by adding the following code at the start of your "go" method.

 System.setProperty("http.proxyHost", "proxy.****.com");
System.setProperty("http.proxyPort", "####");
 Authenticator authenticator = new Authenticator() {

                public PasswordAuthentication getPasswordAuthentication() {
                    return (new PasswordAuthentication("user",
                            "PASSWORD".toCharArray()));
                }
            };
            Authenticator.setDefault(authenticator);

Now, you tell us, what error you got?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top