使用Java中的Google Translator进行文本到语音转换

时间:2014-05-02 09:17:36

标签: java text-to-speech google-translate

我写了一个非常简单的程序来转换Text,从我的C:\驱动器中名为'writer.txt'的文件输入到使用Google Translator的语音。一切正常,我在一个名为'output.mp3'的mp3文件中得到了正确的输出。但是,我的代码显示1错误,我无法调试。请任何人帮助我......

这是我的代码。

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;
        }
    }
}

提前致谢。

1 个答案:

答案 0 :(得分:0)

您的代码给出的第一个错误是在行

中找到的FileNot

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

我希望你的本地机器上有这个文件。

接下来,该文件需要翻译一些数据。

我收到的最后一个错误是拒绝连接。这将通过在“go”方法的开头添加以下代码来解决。

 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);

现在,您告诉我们,您遇到了什么错误?