将文本转换为语音java代码

时间:2012-12-02 15:54:52

标签: java text-to-speech

我不明白这个语法错误insert ")" to complete MethodInvocation Syntax error, insert ";" to complete Statement at demojsapi.main(demojsapi.java:46)

我实际上正在使用以下代码处理将文本转换为speesh的java项目:

import javax.speech.*;
import java.util.*;
import javax.speech.synthesis.*;

public class demojsapi
{
    String speaktext;

    public void dospeak(String speak,String  voicename)
    {
        speaktext=speak;
        String voiceName =voicename;
        try
        {
            SynthesizerModeDesc desc = new SynthesizerModeDesc(null,"general",  Locale.US,null,null);
            Synthesizer synthesizer =  Central.createSynthesizer(desc);
            synthesizer.allocate();
            synthesizer.resume();
            desc = (SynthesizerModeDesc)  synthesizer.getEngineModeDesc();
            Voice[] voices = desc.getVoices();
            Voice voice = null;
            for (int i = 0; i < voices.length; i++)
            {
                if (voices[i].getName().equals(voiceName))
                {
                    voice = voices[i];
                    break;
                }
            }
            synthesizer.getSynthesizerProperties().setVoice(voice);
            synthesizer.speakPlainText(speaktext, null);
            synthesizer.waitEngineState(Synthesizer.QUEUE_EMPTY);
            synthesizer.deallocate();
        }
        catch (Exception e)
        {
            String message = " missing speech.properties in " + System.getProperty("user.home") + "\n";
            System.out.println(""+e);
            System.out.println(message);
        }
    }

    public static void main(String[] args)
    {
        demojsapi obj=new demojsapi(); obj.despeak("shit","kevin16");
    }
}

3 个答案:

答案 0 :(得分:5)

despeak方法不存在。您应该使用常规引号来调用dospeak

obj.dospeak("foo", "kevin16");

答案 1 :(得分:5)

请从指定链接下载谷歌的freetts-1.2.2-bin和

http://sourceforge.net/projects/freetts/?source=directory

在net beans中尝试我的程序

不要忘记将所有jar文件添加到库文件夹中 代码如下。它的工作.. 我的方法---

lazy val hdfsNameNode = settingKey[String]("Namenode for the HDFS access")

val genHdfsConfig = Def.task {
  val files = ((baseDirectory.value / "project" / "templates" / "resources" / "hdfs" ) * "*.xml").get
  files.map({ hdfsTemplate =>
    val config = IO.read(hdfsTemplate).replace("{{namenode}}", hdfsNameNode.value)
    val outputPath = resourceManaged.value / hdfsTemplate.getName
    IO.write(outputPath, config)
    outputPath
  })
}

Seq(
    hdfsNameNode := "undefined", // Only this one is accepted
    hdfsNameNode in Test := "localhost",
    hdfsNameNode in Compile := "172.31.32.228",
    resourceGenerators in Compile += genHdfsConfig.taskValue,
    resourceGenerators in Test += genHdfsConfig.taskValue
  )

从内部代码中调用此方法..

答案 2 :(得分:1)

非常好地解释了运行代码的完整程序 - Text To Speech in Java Using Freetts

工作代码:

import javax.speech.*;    
import java.util.*;    
import javax.speech.synthesis.*;    

public class Text2Speech    
{    
String speaktext; 
public void dospeak(String speak,String  voicename)    
{    
    speaktext=speak;    
    String voiceName =voicename;    
    try    
    {    
        SynthesizerModeDesc desc = new SynthesizerModeDesc(null,"general",  Locale.US,null,null);    
        Synthesizer synthesizer =  Central.createSynthesizer(desc);    
        synthesizer.allocate();    
        synthesizer.resume();     
        desc = (SynthesizerModeDesc)  synthesizer.getEngineModeDesc();     
        Voice[] voices = desc.getVoices();      
        Voice voice = null;
        for (int i = 0; i < voices.length; i++)    
        {    
            if (voices[i].getName().equals(voiceName))    
            {    
                voice = voices[i];    
                break;     
            }     
        }    
        synthesizer.getSynthesizerProperties().setVoice(voice);    
        System.out.print("Speaking : "+speaktext);    
        synthesizer.speakPlainText(speaktext, null);    
        synthesizer.waitEngineState(Synthesizer.QUEUE_EMPTY);    
        synthesizer.deallocate();    
    }    
    catch (Exception e)   
    {    
        String message = " missing speech.properties in " + System.getProperty("user.home") + "\n";    
        System.out.println(""+e);    
        System.out.println(message);    
    }    
}    

public static void main(String[] args)    
{    
    Text2Speech obj=new Text2Speech(); obj.dospeak("Hello i am kevin ","kevin16");    
}    
}