Friday, September 12, 2014

JavaFX with text-to-speech, with java-google-translate-text-to-speech

To add text-to-speech function to JavaFX application, we can using java-google-translate-text-to-speech, an Api unofficial with the main features of Google Translate in Java.

This video show how to add download and add the library of java-google-translate-text-to-speech tyo Netbeans, and create Hello World with text-of-speech in Netbeans.


package javafx_texttospeech;
import com.gtranslate.Audio;
import com.gtranslate.Language;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javazoom.jl.decoder.JavaLayerException;
/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class JavaFX_TextToSpeech extends Application {
 
 @Override
 public void start(Stage primaryStage) {
 Button btn = new Button();
 btn.setText("Say 'Hello World'");
 btn.setOnAction(new EventHandler<ActionEvent>() {
 
 @Override
 public void handle(ActionEvent event) {
 InputStream sound = null;
 try {
 System.out.println("Hello World!");
 Audio audio = Audio.getInstance();
 sound = audio.getAudio("Hello World", Language.ENGLISH);
 audio.play(sound);
 } catch (IOException | JavaLayerException ex) {
 Logger.getLogger(JavaFX_TextToSpeech.class.getName()).log(Level.SEVERE, null, ex);
 } finally {
 try {
 sound.close();
 } catch (IOException ex) {
 Logger.getLogger(JavaFX_TextToSpeech.class.getName()).log(Level.SEVERE, null, ex);
 }
 }
 }
 });
 
 StackPane root = new StackPane();
 root.getChildren().add(btn);
 
 Scene scene = new Scene(root, 300, 250);
 
 primaryStage.setTitle("Hello World!");
 primaryStage.setScene(scene);
 primaryStage.show();
 }
 /**
 * @param args the command line arguments
 */
 public static void main(String[] args) {
 launch(args);
 }
 
}


- Google Translater (with tts) on JavaFX

2 comments:

  1. i had this problem
    java.io.IOException: Server returned HTTP response code: 503 for URL: http://ipv4.google.com/sorry/index?continue=http://translate.google.com/translate_tts%3Fq%3DHello%2520World%26tl%3Den&q=EgQpaGx7GLCk48gFIhkA8aeDS3ZFN5ywSACIRuTwZLp8U4GiZjoOMgNyY24

    Reply Delete
  2. how can i pause audio when it playing, please help?

    Reply Delete

Subscribe to: Post Comments (Atom)

AltStyle によって変換されたページ (->オリジナル) /