【发布时间】: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