JACOB 就是 JAVA-COM Bridge的缩写,语音播报。demo
将jacob-1.14.3-x86.dll或放入32位JDK的bin目录下
或将jacob-1.14.3-x64.dll或放入64位JDK的bin目录下
百度网盘资源链接:https://pan.baidu.com/s/1eRsEubrfFJrMm4oyB3BrQQ
提 取 码:v77k
依赖:
<!-- https://mvnrepository.com/artifact/net.sf.jacob-project/jacob -->
<dependency>
<groupId>net.sf.jacob-project</groupId>
<artifactId>jacob</artifactId>
<version>1.14.3</version>
</dependency>
导入包
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
public class JacobUtil {
public static void main(String[] args) throws Exception {
talkString("靓仔,很高兴认识您。");
}
public static void talkString(String talk) {
ActiveXComponent sap = new ActiveXComponent("Sapi.SpVoice");
try {
// 音量 0-100
sap.setProperty("Volume", new Variant(100));
// 语音朗读速度 -10 到 +10
sap.setProperty("Rate", new Variant(-2));
// 获取执行对象
Dispatch sapo = sap.getObject();
// 执行朗读
Dispatch.call(sapo, "Speak", new Variant(talk));
// 关闭执行对象
sapo.safeRelease();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭应用程序连接
sap.safeRelease();
}
}
public static void talkText(String path) throws Exception {
ActiveXComponent sap = new ActiveXComponent("Sapi.SpVoice");
// 输入文件
File srcFile = new File(path);
// 使用包装字符流读取文件
BufferedReader br = new BufferedReader(new FileReader(srcFile));
String content = br.readLine();
try {
// 音量 0-100
sap.setProperty("Volume", new Variant(100));
// 语音朗读速度 -10 到 +10
sap.setProperty("Rate", new Variant(-1));
// 获取执行对象
Dispatch sapo = sap.getObject();
// 执行朗读
while (content != null) {
Dispatch.call(sapo, "Speak", new Variant(content));
content = br.readLine();
}
// 关闭执行对象
sapo.safeRelease();
} catch (Exception e) {
e.printStackTrace();
} finally {
br.close();
// 关闭应用程序连接
sap.safeRelease();
}
}
}
希望对你有用。