I am trying to use the java built in speech however whenever I try to use it it gives me a null pointer exception when I call synth.allocate. I have looked at examples online but I still get the same error and I can't see what I'm doing wrong

JButton button = new JButton();
    button.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub
            try {
                Synthesizer synth = Central.createSynthesizer(
                    new SynthesizerModeDesc(Locale.ENGLISH));

                synth.allocate();
                synth.resume();

                synth.speakPlainText("Hello, world!", null);

                synth.waitEngineState(Synthesizer.QUEUE_EMPTY);

                synth.deallocate();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
有帮助吗?

解决方案

It's likely that your system doesn't have a synthesizer for Locale.ENGLISH installed. This in turn would cause Central.createSynthesizer() to return null, which would cause the null pointer exception at synth.allocate().

You may want to call Central.availableSynthesizers() to find what synthesizers are installed on your system, before you call Central.createSynthesizer().

Also, as a quick test, you might try Locale.US instead of Locale.ENGLISH and see whether that works...it's just a guess.

I see that FreeTTS (http://freetts.sourceforge.net/docs/index.php) is described as an implementation of the Java Speech interface. Installing that might help.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top