目的
使用Java程序实现朗读文字。
依赖
- jacob.jar(我的是\jacob-1.18)这个可以用Maven去下载(下载后再到本地的库拿到jar),在dependencies节点下增加如下内容:
<!-- https://mvnrepository.com/artifact/com.jacob/jacob 文字转语音 -->
<dependency>
<groupId>com.hynnet</groupId>
<artifactId>jacob</artifactId>
<version>1.18</version>
</dependency>
-
jacob-1.18-x64.dll文件,通过下面的链接获取:
下载链接 -
把jacob-1.18-x64.dll文件复制到jdk的bin目录下
代码
import部分:
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
实现代码:
public static void main(String[] args) {
String content = "早上好";
voicing(content);
}
public static void voicing(String content) {
ActiveXComponent sap = new ActiveXComponent("Sapi.SpVoice");
Dispatch dispatch = sap.getObject();
try {
// 音量 0-100
sap.setProperty("Volume", new Variant(80));
// 语音朗读速度 -10 到 +10
sap.setProperty("Rate", new Variant(-2));
// 执行朗读
Dispatch.call(dispatch, "Speak", new Variant(content));
} catch (Exception e) {
e.printStackTrace();
} finally {
dispatch.safeRelease();
sap.safeRelease();
}
}