A
A
Alex vak2017-12-03 00:30:16
Google
Alex vak, 2017-12-03 00:30:16

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 JLayerfor 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");
  }
}

Class 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 explorerlooks like this: I
image.png
start the program by executing Run as → Java Application on Main.java.
At the bottom of Eclipse in the console I get this error:
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)

I'm having similar problems. JRE4png.png
When I click on, for example, . How to make this text-to-speech (TTS) work in Java, what changes should I make to it? HttpURLConnection.java:1894

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikolai Styazhkin, 2018-07-29
@elgringo

Google seems to have closed access to their TTS

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question