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-100Dispatch.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文件夹