<!-- https://mvnrepository.com/artifact/com.jacob/jacob 文字轉語音 --> <dependency> <groupId>com.hynnet</groupId> <artifactId>jacob</artifactId> <version>1.18</version> </dependency>
https://files.cnblogs.com/files/w1441639547/jacob-1.18-x64.rar
package com.Interface.util; import com.jacob.activeX.ActiveXComponent; import com.jacob.com.Dispatch; import com.jacob.com.Variant; /** * 文字轉語音測試 jdk bin文件中須要導入jacob-1.17-M2-x64.dll * * @author zk * @date: 2019年6月25日 上午10:05:21 */ public class jacobtest { /** * 語音轉文字並播放 * * @param txt */ public static void textToSpeech(String text) { ActiveXComponent ax = null; try { ax = new ActiveXComponent("Sapi.SpVoice"); // 運行時輸出語音內容 Dispatch spVoice = ax.getObject(); // 音量 0-100 ax.setProperty("Volume", new Variant(100)); // 語音朗讀速度 -10 到 +10 ax.setProperty("Rate", new Variant(-2)); // 執行朗讀 Dispatch.call(spVoice, "Speak", new Variant(text)); // 下面是構建文件流把生成語音文件 ax = new ActiveXComponent("Sapi.SpFileStream"); Dispatch spFileStream = ax.getObject(); ax = new ActiveXComponent("Sapi.SpAudioFormat"); Dispatch spAudioFormat = ax.getObject(); // 設置音頻流格式 Dispatch.put(spAudioFormat, "Type", new Variant(22)); // 設置文件輸出流格式 Dispatch.putRef(spFileStream, "Format", spAudioFormat); // 調用輸出 文件流打開方法,建立一個.wav文件 Dispatch.call(spFileStream, "Open", new Variant("./text.wav"), new Variant(3), new Variant(true)); // 設置聲音對象的音頻輸出流爲輸出文件對象 Dispatch.putRef(spVoice, "AudioOutputStream", spFileStream); // 設置音量 0到100 Dispatch.put(spVoice, "Volume", new Variant(100)); // 設置朗讀速度 Dispatch.put(spVoice, "Rate", new Variant(-2)); // 開始朗讀 Dispatch.call(spVoice, "Speak", new Variant(text)); // 關閉輸出文件 Dispatch.call(spFileStream, "Close"); Dispatch.putRef(spVoice, "AudioOutputStream", null); spAudioFormat.safeRelease(); spFileStream.safeRelease(); spVoice.safeRelease(); ax.safeRelease(); } catch (Exception e) { e.printStackTrace(); } } }