Answer the question
In order to leave comments, you need to log in
How to make text-to-speech (TTS) work in Java?
Hello.
My questions (there are several of them and they are in bold ) go below. First a short introduction.
I'm currently learning Java (using Eclipse) and came across the following tutorial - which is a variation of the Java text-to-speech (TTS) program. Actually the program itself is here Lesson01_Speech.zip .
The essence of the program is as follows:
1. Link the project with the library JLayer
for playing mp3 with java (Using the library jl1.0.1.jar
).
2. Create an instance of the class: .
3. Use method: . The first argument is the phrase to pronounce, the second is the language.GoogleTextToSpeech gtts = new GoogleTextToSpeech()
gtts.say("Hello everybody", "en")
I added my comments to the program and commented out the obviously superfluous line, now the program looks like this (class Main
):
package com.example.TTS; // пакет в котором лежит класс Main
import com.example.TTS.GoogleTextToSpeech; // импорт класса GoogleTextToSpeech
public class Main {
public static void main(String[] args) {
GoogleTextToSpeech gtts = new GoogleTextToSpeech(); // Создать экземпляр класса
gtts.say("Hello dear friends", "en"); // Использовать метод
// gtts.say("Bonjour mon amis!", "fr");
}
}
GoogleTextToSpeech
:package com.example.TTS; // пакет в котором лежит класс GoogleTextToSpeech
import java.io.InputStream; // импорт библ. классов
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import javazoom.jl.player.Player; // импорт класса из jl1.0.1.jar
public class GoogleTextToSpeech {
private static String ENCODING = "UTF-8"; // создание констант
private static String URL_BEGINNING = "http://translate.google.com/translate_tts?ie=";
private static String URL_QUERY = "&q=";
private static String URL_TL = "&tl=";
private static String USER_AGENT_LITERAL = "User-Agent";
private static String USER_AGENT = "Mozilla/4.7";
public void say( String phrase, String lang ) {
try {
//Make full URL
phrase=URLEncoder.encode(phrase, ENCODING);
String sURL = URL_BEGINNING + ENCODING + URL_TL + lang + URL_QUERY + phrase;
URL url = new URL(sURL);
//Create connection
URLConnection urlConn = url.openConnection();
HttpURLConnection httpUrlConn = (HttpURLConnection) urlConn;
httpUrlConn.addRequestProperty(USER_AGENT_LITERAL, USER_AGENT);
//Create stream
InputStream mp3WebStream = urlConn.getInputStream();
//Play stream
Player plr = new Player(mp3WebStream);
plr.play();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
Package explorer
looks like this: I Run as → Java Application
on Main.java
. java.io.IOException: Server returned HTTP response code: 503 for URL: http://ipv4.google.com/sorry/index?continue=http://translate.google.com/translate_tts%3Fie%3DUTF-8%26tl%3Den%26q%3DHello%2Bdear%2Bfriends&q=EgRbegWvGJ3FldEFIhkA8aeDS952qU94sbK8DAgP55pH8cbMicZdMgFy
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1894)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492)
at com.example.TTS.GoogleTextToSpeech.say(GoogleTextToSpeech.java:32)
at com.example.TTS.Main.main(Main.java:9)
HttpURLConnection.java:1894
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question