JAVA 实现Jacob语音播报

准备工作:下载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

扫描二维码关注公众号,回复: 6707026 查看本文章

mvn install:install-file -Dfile=jar包地址 -DgroupId=mavengroupId仓库目录 -DartifactId=artifactId目录 -Dversion=version版本
 -Dpackaging=jar

POM文件

 <dependency>
            <groupId>cn.jacob</groupId>
            <artifactId>jacob</artifactId>
            <version>1.19</version>
 </dependency>

javaDemo

package com;


import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class TestJacob {
    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(-2));

// 获取执行对象

            Dispatch sapo = sap.getObject();

// 执行朗读

            Dispatch.call(sapo, "Speak", new Variant("请A10012,到四号窗口就诊。"));

// 关闭执行对象

            sapo.safeRelease();

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

// 关闭应用程序连接

            sap.safeRelease();

        }

    }

}

demo2

package com.jeeplus.common.utils;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;


import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;


public class TalkUtil {
    public static void main(String[] args) throws Exception {
        talkString("你好,很高兴认识你。");
        // talkText("C:/aa.txt");


    }


    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();
        }
    }


}

猜你喜欢

转载自www.cnblogs.com/kkxwze/p/11123708.html