1,Jar包下载
链接:https://pan.baidu.com/s/1CrrLni304LOu_r9is8WRhA
提取码:wyi4
2,引入jar
直接拷贝jacob.jar到项目的lib文件夹,然后Build Path;
如果是maven项目,需将jacob.jar引入本地maven仓库,然后添加依赖(方法:https://www.cnblogs.com/3b2414/p/11927455.html);
3,添加dll
我的JDK是32位的,所以在jdk目录下的jre目录下的bin中添加jacob-1.19-x86.dll(如果是64位,则添加jacob-1.19-x64.dll);
4,demo
public static void main(String[] args) { // 创建与微软应用程序的新连接。传入的参数是注册表中注册的程序的名称。 ActiveXComponent sap = new ActiveXComponent("Sapi.SpVoice"); try { // 音量 0-100 sap.setProperty("Volume", new Variant(100)); // 语音朗读速度 -10 到 +10 sap.setProperty("Rate", new Variant(0)); // 获取执行对象 Dispatch sapo = sap.getObject(); // 执行朗读 Dispatch.call(sapo, "Speak", new Variant("数据出现异常波动")); // 关闭执行对象 sapo.safeRelease(); } catch (Exception e) { e.printStackTrace(); } finally { // 关闭应用程序连接 sap.safeRelease(); } } public static void jacobText(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(0)); // 获取执行对象 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(); } }