Java使用Jacob调用SAPI合成语音 tts

Java使用Jacob调用SAPI合成语音: 

1.配置Jacob:我这里使用的jacob为jacob-1.18-M2,jacob-1.18-M2下载下来之后,解压缩文件,
里面的文件为docs、jacob.jar、jacob-1.18-M2-x64.dll、jacob-1.18-M2-x86.dll、LICENSE.TXT、README.txt;
把jacob.jar复制到项目的lib目录下面,jacob-1.18-M2-x64.dll和jacob-1.18-M2-x86.dll是两个动态库文件,
它们指JDK的安装版本,如果JDK是64位的,那么需要把jacob-1.18-M2-x64.dll复制到JRE BIN目录下面,因为
我装的JDK是32位的,所以我把jacob-1.18-M2-x86.dll复制到JRE BIN目录下面,
我的JRE路径为G:\Program Files (x86)\Java\jre7\bin。 

2.代码如下

  1. package com.jacob;  
  2.   
  3. import com.jacob.activeX.ActiveXComponent;  
  4. import com.jacob.com.ComThread;  
  5. import com.jacob.com.Dispatch;  
  6. import com.jacob.com.Variant;  
  7.   
  8. /** 
  9.  * 调用MSTTS将字符串转换成语音信息 
  10.  * 调用windows speech API(SAPI) 
  11.  * @author suyunlong 
  12.  * 
  13.  */  
  14. public class MSTTSSpeech  
  15. {  
  16.     private int volume=100;// 声音:1到100  
  17.     private int rate=0;// 频率:-10到10  
  18.     private int voice=0;// 语音库序号  
  19.     private int audio=0;// 输出设备序号  
  20.     private ActiveXComponent ax=null;  
  21.     private Dispatch spVoice=null;// 声音对象  
  22.     private Dispatch spFileStream=null;// 音频文件输出流对象,在读取或保存音频文件时使用  
  23.     private Dispatch spAudioFormat=null;// 音频格式对象  
  24.     private Dispatch spMMAudioOut=null;// 音频输出对象  
  25.     private int formatType=22;// 音频的输出格式,默认为:SAFT22kHz16BitMono  
  26.     public MSTTSSpeech()   
  27.     {  
  28.         ComThread.InitSTA();  
  29.         if(ax==null)  
  30.         {  
  31.             ax=new ActiveXComponent("Sapi.SpVoice");  
  32.             spVoice=ax.getObject();  
  33.         }  
  34.     }  
  35.     /** 
  36.      * 改变语音库 
  37.      * @param voice 语音库序号 
  38.      */  
  39.     public void changeVoice(int voice)  
  40.     {  
  41.         if(this.voice != voice)  
  42.         {  
  43.             this.voice=voice;  
  44.         }  
  45.         try  
  46.         {  
  47.             Dispatch voiceItems=Dispatch.call(spVoice,"GetVoices").toDispatch();  
  48.             int count=Integer.valueOf(Dispatch.call(voiceItems,"Count").toString());  
  49.             if(count>0)  
  50.             {  
  51.                 Dispatch voiceItem=Dispatch.call(voiceItems,"Item",new Variant(this.voice)).toDispatch();  
  52.                 Dispatch.put(spVoice,"Voice",voiceItem);  
  53.             }  
  54.         }  
  55.         catch(Exception e)  
  56.         {  
  57.             System.out.println(e.getMessage());  
  58.             e.printStackTrace();  
  59.         }  
  60.     }  
  61.     /** 
  62.      * 改变音频输出设备 
  63.      * @param audio 音频设备序号 
  64.      */  
  65.     public void changeAudioOutput(int audio)  
  66.     {  
  67.         if(this.audio != audio)  
  68.         {  
  69.             this.audio=audio;  
  70.         }  
  71.         try  
  72.         {  
  73.             Dispatch audioOutputs=Dispatch.call(spVoice,"GetAudioOutputs").toDispatch();  
  74.             int count=Integer.valueOf(Dispatch.call(audioOutputs,"Count").toString());  
  75.             if(count > 0)  
  76.             {  
  77.                 Dispatch audioOutput=Dispatch.call(audioOutputs,"Item",new Variant(this.audio)).toDispatch();  
  78.                 Dispatch.put(spVoice,"AudioOutput",audioOutput);  
  79.             }  
  80.         }  
  81.         catch(Exception e)  
  82.         {  
  83.             System.out.println(e.getMessage());  
  84.             e.printStackTrace();  
  85.         }  
  86.     }  
  87.     /** 
  88.      * 播放语音  
  89.      * @param text 要转换成语音的文本 

  90.      */
Last modification:January 1st, 1970 at 08:00 am
If you think my article is useful to you, please feel free to appreciate

Leave a Comment