文本到语音转换器不工作

时间:2016-06-28 10:18:22

标签: java text-to-speech

我正在尝试使用java代码进行文本到语音转换。我正在使用freetts.jar来执行此操作。我需要在我的Web应用程序中使用它。

import com.sun.speech.freetts.*;
public class convert {
    private static final String VOICENAME="kevin";

    public static void call(){
    Voice voice;

    VoiceManager vm=VoiceManager.getInstance();
    System.out.println("come");
    voice=vm.getVoice(VOICENAME);

    voice.allocate();

    try{

        voice.speak("wellcome to my world");
        System.out.println("coming here good"); 
    }
    catch(Exception e){
        System.out.println(e);
    }
    }
    public static void main(String agrs[]){

        call();


    }

}

在上面的代码中没有工作 voice.speak()方法无效。我不知道为什么。任何人都可以帮我解决这个问题吗?

此外,我还需要知道如何使用自己的声音进行文本转换。 谢谢

2 个答案:

答案 0 :(得分:0)

代码没有问题。你必须只将freetts.jar添加到你的构建路径中。它将给出nullpointer异常。

将freetts-1.2.2-bin的lib文件夹中的所有jar添加到构建路径中。

相同的代码对我有用。

试着告诉我。

为了实现自己的声音:查看这些文章

http://www.codeproject.com/Articles/182881/Text-to-Speech?msg=5074134#xx5074134xx

http://www.acapela-group.com/voices/voice-replacement/faq-my-own-voice/

答案 1 :(得分:0)

我知道我发帖很晚,但这可能对某人有所帮助。我试过类似的,它对我有用。请找到以下代码。

    package com.mani.texttospeech;

import java.beans.PropertyVetoException;
import java.util.Locale;

import javax.speech.AudioException;
import javax.speech.Central;
import javax.speech.EngineException;
import javax.speech.EngineStateError;
import javax.speech.synthesis.Synthesizer;
import javax.speech.synthesis.SynthesizerModeDesc;
import javax.speech.synthesis.Voice;

/**
 *
 * @author Manindar
 */
public class SpeechUtils {

    SynthesizerModeDesc desc;
    Synthesizer synthesizer;
    Voice voice;

    public void init(String voiceName) throws EngineException, AudioException, EngineStateError, PropertyVetoException {
        if (desc == null) {
            System.setProperty("freetts.voices", "com.sun.speech.freetts.en.us.cmu_us_kal.KevinVoiceDirectory");
            desc = new SynthesizerModeDesc(Locale.US);
            Central.registerEngineCentral("com.sun.speech.freetts.jsapi.FreeTTSEngineCentral");
            synthesizer = Central.createSynthesizer(desc);
            synthesizer.allocate();
            synthesizer.resume();
            SynthesizerModeDesc smd = (SynthesizerModeDesc) synthesizer.getEngineModeDesc();
            Voice[] voices = smd.getVoices();
            for (Voice voice1 : voices) {
                if (voice1.getName().equals(voiceName)) {
                    voice = voice1;
                    break;
                }
            }
            synthesizer.getSynthesizerProperties().setVoice(voice);
        }
    }

    public void terminate() throws EngineException, EngineStateError {
        synthesizer.deallocate();
    }

    public void doSpeak(String speakText) throws EngineException, AudioException, IllegalArgumentException, InterruptedException {
        synthesizer.speakPlainText(speakText, null);
        synthesizer.waitEngineState(Synthesizer.QUEUE_EMPTY);
    }

    public static void main(String[] args) throws Exception {
        SpeechUtils su = new SpeechUtils();
        su.init("kevin16");
//        su.init("kevin");
//        su.init("mbrola_us1");
//        su.init("mbrola_us2");
//        su.init("mbrola_us3");
        // high quality
        su.doSpeak("Hi this is Manindar. Welcome to audio world.");
        su.terminate();
    }
}

并将以下依赖项添加到 pom.xml 文件中。

<dependencies>
        <dependency>
            <groupId>net.sf.sociaal</groupId>
            <artifactId>freetts</artifactId>
            <version>1.2.2</version>
        </dependency>
    </dependencies>

希望这会有所帮助。