Sylwia Dorota Kedzia Sylwia is a developer at IDR Solutions working on JavaFX Development and on the NetBeans JavaFX Plugin. She is very passionate about programming and aims to improve constantly.

How to enable Text to Speech in JavaFX and Swing Applications

1 min read

As a developer at IDR Solutions I spend a lot of my time working on the JPedal Java PDF library and lately I have been improving functionality with text to speech. I thought it might be useful to show you how to implement text to speech in both JavaFX and Swing applications in this tutorial.

JavaFX

1. Download jars:

For the JavaFX application we are using the java-google-translate-text-to-speech jars.

JavaFX jars

Download both of the jars and add them to your JavaFX application:

JavaFX libraries

2. Create GUI and add the functionality to the button:

TextToSpeach JavaFX

button.setOnAction(new EventHandler<ActionEvent>() {
 
    @Override
    public void handle(ActionEvent event) {
        Audio audio = Audio.getInstance();
        InputStream sound = null;
        try {
        sound = audio.getAudio(text.getText(), Language.ENGLISH);
        } catch (IOException ex) {
        Logger.getLogger(JavaFX_TextToSpeech.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
        audio.play(sound);
        } catch (JavaLayerException ex) {
        Logger.getLogger(JavaFX_TextToSpeech.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
});

Using java-google-translate-text-to-speech jars you can also build simple translator where you can translate your content into other languages.

Java Swing

1. Download jars:

For the Swing application we are using the FreeTTS jars.

4

Download the freets-1.2.2-bin.zip. Unzip the folder and add the jars from \freetts-1.2.2-bin\freetts-1.2\lib location to your application.

Swing libraries

2. Create GUI and add the functionality to the button:

swing gui

 button.addActionListener(new ActionListener() {
 
    public void actionPerformed(ActionEvent ae) {
        VoiceManager vm = VoiceManager.getInstance();
        Voice voice = vm.getVoice("kevin16");
 
        voice.allocate();
 
        voice.speak(text.getText());
    }
});

To find out how to implement text to speech functionality in our JPedal Java PDF Viewer click here.

Hopefully you found this guide useful. Let us know if you would like to see anymore like this in the future.

If you’re a first-time reader, or simply want to be notified when we post new articles and updates, you can keep up to date by social media (Twitter, Facebook and Google+) or the Blog RSS.

 

 

IDRsolutions develop a Java PDF Viewer and SDK, an Adobe forms to HTML5 forms converter, a PDF to HTML5 converter and a Java ImageIO replacement. On the blog our team post anything interesting they learn about.

Sylwia Dorota Kedzia Sylwia is a developer at IDR Solutions working on JavaFX Development and on the NetBeans JavaFX Plugin. She is very passionate about programming and aims to improve constantly.

2 Replies to “How to enable Text to Speech in JavaFX and…”

  1. I am getting the following:-

    java.io.IOException: Server returned HTTP response code: 503 for URL: http://ipv4.google.com/sorry/IndexRedirect?continue=http://translate.google.com/translate_tts%3Fq%3DHello%2520World%26tl%3Den&q=CGMSBCnes6gY3Ma9vAUiGQDxp4NLtQVq8cs6ppwO6rZqdlfDtNq3DDM
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1840)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441)
    at com.gtranslate.Audio.getAudio(Audio.java:34)

Leave a Reply

Your email address will not be published. Required fields are marked *