【问题标题】:Text to Speech using Google Translator in Java [closed]在 Java 中使用谷歌翻译器进行文本到语音 [关闭]
【发布时间】:2014-05-02 09:17:36
【问题描述】:

我编写了一个非常简单的程序来使用谷歌翻译从我的 C:\ 驱动器中名为“writer.txt”的文件中输入的文本转换为语音。一切正常,我在名为“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;
        }
    }
}

提前致谢。

【问题讨论】:

  • 还有什么错误??
  • @user1932914:你的问题是什么
  • @user1932914 - 请连同问题一起提供您的错误。
  • 线程“main”中的异常 java.io.FileNotFoundException: translate.google.com/translate_tts?tl=en&q= at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1834) at sun.net.www .protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1439) at tts.TTS.go(TTS.java:80) at tts.TTS.main(TTS.java:55) Java 结果:1 我收到此错误运行上面的代码时..如何解决这个错误,我从上面的代码中得到了想要的输出。

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


【解决方案1】:

您的代码给出的第一个错误是 FileNot found in the line

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

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

【讨论】:

  • 我没有收到前两个错误,因为我在指定文件夹中已经有一个包含一些数据的文件。我得到了第三个错误,你能解释一下你做了什么以及为什么会出现这样的错误。
  • 你得到这个错误意味着你试图从代理后面执行代码(你在办公室吗?)。该代码负责代理身份验证。它解决了你的目的吗?如果是,请采纳答案。
  • 我在我的主类中的“go”语句之前添加了您的代码,但仍然遇到相同的错误......我试图将它放在循环内部和外部,但同样的错误又来了。 ..我不在办公室,我在家里。
  • 我希望您相应地更改代理 url、端口、用户和密码?
  • 这很有帮助,谢谢 Hirak。如果你喜欢我的问题,请投票给它......
猜你喜欢
  • 2014-01-18
  • 1970-01-01
  • 2012-06-16
  • 2012-10-31
  • 2016-08-25
  • 1970-01-01
  • 1970-01-01
  • 2013-05-06
  • 2023-03-04
相关资源
最近更新 更多