0

I have a java program in which I am using TTS (Text to speech), in there along with that I want an animated gif to come up on the screen. I am using Netbeans GUI builder, so I made up a new jpanel form and added the gif in a label in that form (java jpanel form), and after that I added this java as a jpanel to my main java file (by dragging and dropping). But the problem is that as soon as the TTS, starts to speak something it stops the animation of the gif. How to make it work together? Note: I am using freeTTS for converting text to speech

Code:

private static final String VOICENAME = "kevin16";
VoiceManager voiceManager = VoiceManager.getInstance();
.....
........
Voice voice;
voice = voiceManager.getVoice(VOICENAME);
voice.allocate();
....//Some code here
t4.setText("" + ran);  
voice.speak(t4.getText()); 
listenanum.setText("" + d);
listenanum.setVisible(false);
Daksh Shah
  • 2,997
  • 6
  • 37
  • 71
  • 1
    We're missing a few pieces: "which API and implementation are you using for your TTS?" and "how are you invoking TTS?". In particular, if you are invoking TTS from the Swing UI thread, and your TTS API blocks the thread while it is speaking, nothing in the UI will update - if that's the case you need to speak on a different thread. – Erwin Bolwidt May 17 '14 at 05:21
  • @ErwinBolwidt I don't know all what you wrote but I will try my best to give you that information, I will show the code by which I am doing TTS – Daksh Shah May 17 '14 at 05:21
  • @ErwinBolwidt What else do you want? – Daksh Shah May 17 '14 at 05:27
  • Note I didn't vote on the question. One thing that is likely but I'm not 100% sure about - how is this code invoked? Is it from an ActionListener or other event listener? – Erwin Bolwidt May 17 '14 at 05:38
  • @ErwinBolwidt I am not sure but i think ActionListener because this code is in a button that is called from timer, final Timer timer = new Timer(zad, new ActionListener() { So i think what you are asking is actionlistener. If not, what to search for? I have made most of it with GUI so i dont have much idea – Daksh Shah May 17 '14 at 05:42

1 Answers1

3

I had to look up the FreeTTS Javadoc, which confirms what I thought.

The speak method on Voice blocks until the spoken text is complete. The method speak(String) invokes speak(FreeTTSSpeakable speakable), which has this Javadoc:

Speak the given queue item. This is a synchronous method that does not return until the speakable is completely spoken or has been cancelled.

However, in Swing, as long as you're doing one thing in the UI thread, it cannot do anything else. So your animation will stop because it also needs the UI thread to repaint the image.

Perhaps the best way to resolve this is to delve deeper into the speech API and use the processUtterance(Utterance u) method on Voice. This method is asynchronous; it returns immediately while the speech is done on a different speech output thread.

However a simpler solution to get you going is to call the speak method on a different thread.

final String textToSpeak = t4.getText();
Thread speechThread = new Thread(new Runnable() {
    public void run() {
        voice.speak(textToSpeak);
    }
});
speechThread.start();

Actually, it's better to use a thread pool than to start a new thread each time, but that goes beyond your immediate problem. You can search StackOverflow or look at java.util.concurrent.ThreadPoolExecutor for more information.

Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79
  • Why not run animated gif in a separate thread? because I will use TTS in a loop while animated gif is only started once in a while – Daksh Shah May 17 '14 at 16:21
  • You can't use the UI thread for speaking and another thread for drawing - Swing uses a single-threaded model for the UI and requires it for UI interaction. But you can keep all the speech-related work on a separate thread, and when you need to interact with the UI, you can use `EventQueue.invokeLater`. See among many other Q's on SO: http://stackoverflow.com/questions/19167154/why-is-it-important-to-use-invokelater, http://stackoverflow.com/questions/5544447/why-are-most-ui-frameworks-single-threaded – Erwin Bolwidt May 18 '14 at 04:40