java利用jacob,文字转语音文件,windows
public static void testToSpeech(String text) { ActiveXComponent activeXComponent = new ActiveXComponent("Sapi.SpVoice"); //运行时输出语音内容 Dispatch dispatch = activeXComponent.getObject(); //文件名称 try{ //生成语音文件 activeXComponent = new ActiveXComponent("Sapi.SpFileStream"); Dispatch fileStreamDispatch = activeXComponent.getObject(); //音频 activeXComponent = new ActiveXComponent("Sapi.SpAudioFormat"); Dispatch audioDispatch = activeXComponent.getObject(); //设置文件流格式 Dispatch.putRef(fileStreamDispatch, "Format", audioDispatch); //设置音频流格式 Dispatch.put(audioDispatch, "Type", new Variant(22)); //调用输出文件流打开方法,创建一个.wav .mp3 .mp4 .wma文件 Dispatch.call(fileStreamDispatch, "Open", new Variant("C:\call.wav"),new Variant(3),new Variant(true)); //设置声音对象的音频流输出流为输出文件对象 Dispatch.putRef(dispatch, "AudioOutputStream", fileStreamDispatch); //设置音量0-100 Dispatch.put(dispatch, "Volume", new Variant(100)); //设置朗读速度 Dispatch.put(dispatch, "Rate", new Variant(-2)); //开始朗读 Dispatch.call(dispatch, "Speak",new Variant(text)); //关闭输出文件流 Dispatch.call(fileStreamDispatch, "Close"); Dispatch.putRef(dispatch, "AudioOutputStream", null); audioDispatch.safeRelease(); fileStreamDispatch.safeRelease(); dispatch.safeRelease(); activeXComponent.safeRelease(); }catch (Exception e) { e.printStackTrace(); } }
<dependency> <groupId>com.hynnet</groupId> <artifactId>jacob</artifactId> <version>1.18</version> </dependency>
自行找到jacob-1.18-x64.dll,放入jdk安装目录下的bin文件夹