2016-06-28 108 views
0

我正在嘗試使用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()方法是行不通的。我不知道why.can任何一個可以幫助我解決這一問題?

而且我還需要知道如何使文本以自己的聲音進行語音轉換。 謝謝

+2

「我需要在我的web應用程序中使用它。」 - 我真的懷疑這會按預期行事。在web應用程序中使用此代碼,您認爲哪個人的聲音會出現?其次「不工作」不是一個足夠的錯誤描述來幫助你。如果可用,請添加堆棧跟蹤。考慮檢查[問]是否改善問題。 – Fildor

回答

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> 

希望這會有所幫助。