jacob TTS语音库异常ComFailException invoke of: Speak的产生原因,以及解决办法。_activexcomponent ax = new activexcomponent("sapi.s-CSDN博客

问题描述

在本地开发调试语音库都没问题,一部署到机器上就出现下述异常。
这里要注意的是:jacob.jar和jacob.dll文件放置位置,jacob.dll放置C:/windows/System32下,以及jdk的bin目录下,jacob.jar放至jdk的lib目录下。
代码如下:

ActiveXComponent ax1 = new ActiveXComponent("Sapi.SpVoice");
        Dispatch ttd = ax1.getObject();
        Dispatch d = ax1.getProperty("voice").toDispatch();
        Variant vitem = Dispatch.call(d, "GetDescription");
        System.out.println("当前语音库:"+vitem);
        ActiveXComponent ax = null;
        try {
            deleteFile(path);
            ax = new ActiveXComponent("Sapi.SpVoice");
            Dispatch spVoice = ax.getObject();

            ax = new ActiveXComponent("Sapi.SpFileStream");
            Dispatch spFileStream = ax.getObject();

            ax = new ActiveXComponent("Sapi.SpAudioFormat");
            Dispatch spAudioFormat = ax.getObject();

            Dispatch.put(spAudioFormat, "Type", new Variant(22));

            Dispatch.putRef(spFileStream, "Format", spAudioFormat);
            Dispatch.call(spFileStream, "Open", new Variant(path+"/"+fileName+".wav"), new Variant(3), new Variant(true));
            Dispatch.putRef(spVoice, "AudioOutputStream", spFileStream);
            Dispatch.put(spVoice, "Volume", new Variant(100));
            Dispatch.put(spVoice, "Rate", new Variant(-2));
            // 注意这里就是后面出现异常的地方
            Dispatch.call(spVoice, "Speak", new Variant(text));

            Dispatch.call(spFileStream, "Close");
            Dispatch.putRef(spVoice, "AudioOutputStream", null);

            spAudioFormat.safeRelease();
            spFileStream.safeRelease();
            spVoice.safeRelease();
            ax.safeRelease();

异常产生

ComFailException invoke of: Speak
source
description

原因分析

由于我本地系统为win8中文版(正版系统),windows的组件和语音库都有,而部署的机器装的是win7旗舰版,好多功能都被阉割掉了(经查:没了office,没了TTS, 而这个TTS正是我们语音播报的引擎,旗舰版竟然给阉掉了,具体阉没阉你可以在C:\windows\System32\Speech\Engines\TTS目录下查看是否存在en和zh中英文引擎,如果存在,语音播报即正常)。
这里插播一条题外话:微软的TTS引擎中默认使用的是HUIHUI这个girl来播报的,取自注册表regedit,如果你要更改的话去注册表把HUIHUI干了再更换。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

阉割版系统无TTS的解决办法

如果你的系统没有TTS引擎,那你就去找TTS补丁安装,我一开始就是这样搞的,国内站点查了好多好多资料,国外站点看了好多英文博克和社区讨论,都没找到想要的答案,还下载了好多不同版本的补丁,结果都没装好,还引入了好多病毒(国内站点下载东西要深思)
下载地址:https://download.csdn.net/download/u014033756/11644887

那么TTS问题我是怎么解决的

windows自带的TTS引擎出了问题,那不用他了行不行?当然可以。
jacob使用的是windows默认的TTS,既然这块搞起来这么麻烦还不一定搞的定,索性不用jacob(播报的声音也不性感,后来我换了),使用科大讯飞或者百度AI语音,都可以噻。我选择了后者。废话不多少,贴代码
这里使用了下面四个依赖:

aip-java-sdk-4.1.1.jar
jl1.0.1.jar
json-20160810.jar
log4j-1.2.17.jar
  public static final String FILE_SUFFIX = ".mp3";

    //设置APPID/AK/SK   这里要去百度语音注册账号,创建应用生成APP_ID等下面三项
    public static final String APP_ID = "";
    public static final String API_KEY = "";
    public static final String SECRET_KEY = "";

    private static final AipSpeech aipSpeech = getAipSpeech();
    private Player player;
    public static AipSpeech getAipSpeech(){
        // 初始化一个AipSpeech
        AipSpeech aipSpeech = new AipSpeech(APP_ID, API_KEY, SECRET_KEY);
        // 可选:设置网络连接参数
        aipSpeech.setConnectionTimeoutInMillis(2000);
        aipSpeech.setSocketTimeoutInMillis(60000);
        return aipSpeech;
    }

    public boolean getMP3ByText(String text, String filename, String filePath){
        player = null;
        // 设置可选参数
        HashMap<String, Object> options = new HashMap<String, Object>();
        // 语速,取值0-9,默认为5中语速
        options.put("spd", "5");
        // 音调,取值0-9,默认为5中语调
        options.put("pit", "5");
        // 音量,取值0-15,默认为5中音量
        options.put("vol", "5");
        // 发音人选择, 0为女声,1为男声,3为情感合成-度逍遥,4为情感合成-度丫丫,默认为普通女
        options.put("per", "4");
        // 调用接口
        // text 合成的文本,使用UTF-8编码。小于2048个中文字或者英文数字。(文本在百度服务器内转换为GBK后,长度必须小于4096字节)
        // lang 固定值zh。语言选择,目前只有中英文混合模式,填写固定值zh
        // ctp 客户端类型选择,web端填写固定值1
        TtsResponse res = aipSpeech.synthesis(text, "zh", 1, options);
        // 如果合成成功,下行数据为二进制语音文件,包含在data中。 如果合成出现错误,则会填充返回值到result中。
        // 若请求错误,服务器将返回的JSON文本包含以下参数:
        // error_code:错误码。
        // error_msg:错误描述信息,帮助理解和解决发生的错误。
        byte[] data = res.getData();
        JSONObject res1 = res.getResult();
        if (data != null) {
            try {
                Util.writeBytesToFileSystem(data, filePath + filename + FILE_SUFFIX);
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
        }
        if (res1 != null) {
            System.out.println(res1.toString(2));
        }
        return true;
    }
    public void playMP3(String pathAndName){
        try {
            BufferedInputStream buffer = new BufferedInputStream(new FileInputStream(pathAndName));
            player = new Player(buffer);
            player.play();
        } catch (Exception e) {
            System.out.println(e);
        }
    }

结尾

其实TTS播报引擎国内外有好成熟方案供我们选择,不必太纠结一家,不行就换。


原网址: 访问
创建于: 2024-04-24 17:11:41
目录: default
标签: 无

请先后发表评论
  • 最新评论
  • 总共0条评论