准备工作:下载Jar
链接:https://pan.baidu.com/s/1edskJjYrCiefVJ7l3Ul9kQ 提取码:6dg9
---导入jar
解压jar包,将jacob.jar复制到工程目录,右键该文件→Build Path→Add to...
将(64位系统添加)jacob-1.19-M2-x64.dll添加到JDK的bin目录和Windows的system32目录和SysWOW64 (以防出错添加)(32位系统添加jacob-1.19-M2-x86.dll)
添加到SysWOW
添加到System32
如果用Maven jacob.jar上传自己私服,添加到本地Maven仓库中
mvn install:install-file -Dfile=D:\BaiduNetdiskDownload\jacob-1.19\jacob.jar -DgroupId=com.jacob -DartifactId=jacob -Dversion=1.19
-Dpackaging=jar
mvn install:install-file -Dfile=jar包地址 -DgroupId=mavengroupId仓库目录 -DartifactId=artifactId目录 -Dversion=version版本
-Dpackaging=jar
POM文件
cn.jacob
jacob
1.19
javaDemo
packagecom;importcom.jacob.activeX.ActiveXComponent;importcom.jacob.com.Dispatch;importcom.jacob.com.Variant;public classTestJacob {public static voidmain(String[] args) {//创建与微软应用程序的新连接。传入的参数是注册表中注册的程序的名称。
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("请A10012,到四号窗口就诊。"));//关闭执行对象
sapo.safeRelease();
}catch(Exception e) {
e.printStackTrace();
}finally{//关闭应用程序连接
sap.safeRelease();
}
}
}
demo2
packagecom.jeeplus.common.utils;importjava.io.BufferedReader;importjava.io.File;importjava.io.FileNotFoundException;importjava.io.FileReader;importjava.io.IOException;importcom.jacob.activeX.ActiveXComponent;importcom.jacob.com.Dispatch;importcom.jacob.com.Variant;public classTalkUtil {public static void main(String[] args) throwsException {
talkString("你好,很高兴认识你。");//talkText("C:/aa.txt");
}public static voidtalkString(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", newVariant(talk));//关闭执行对象
sapo.safeRelease();
}catch(Exception e) {
e.printStackTrace();
}finally{//关闭应用程序连接
sap.safeRelease();
}
}public static void talkText(String path) throwsException {
ActiveXComponent sap= new ActiveXComponent("Sapi.SpVoice");//输入文件
File srcFile = newFile(path);//使用包装字符流读取文件
BufferedReader br = new BufferedReader(newFileReader(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", newVariant(content));
content=br.readLine();
}//关闭执行对象
sapo.safeRelease();
}catch(Exception e) {
e.printStackTrace();
}finally{
br.close();//关闭应用程序连接
sap.safeRelease();
}
}
}