java使用jacob.jar实现文字转语音

java使用jacob.jar实现文字转语音,第1张

package com.study.springbootdemo.Controller;

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

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 文字转语音测试 jdk bin文件中需要导入jacob-1.18-M2-x64.dll
 *
 * @author zk
 * @date: 2019年6月25日 上午10:05:21
 */
public class Test {


    public static void main(String[] args) {
        String file = "E:\\新建文件夹";

        // String str=“请A001号到3号窗口”;
        // String str=“华为 new bee”;
//    String str = "3号机柜报警,3号机柜报警,3号机柜报警";
        String str = "当月亮和太阳处于地球两侧,并且月亮和太阳的黄经相差180度时,从地球上看,此时的月亮最圆,称之为“满月”,亦称为“望”。农历每月的十四、十五、十六甚至十七,都是满月可能出现的时段。而“超级月亮”指的就是月亮在满月的时候,刚好位于近地点附近。由于靠近地球,所以看上去,此时的月亮比平时更大。“超级月亮”每年都会发生,有时还不止一次。";
        //文字转语音,返回语音保存的地址
        Map map = textToSpeech(str, file);
        //地址不为空时,转换采样率和文件格式
        if (map != null && map.size() > 0) {
            Map map1 = samplingRateTest.testCompressMp3Samll(map.get("filePath").toString(), file, 16000, "mp3");
            System.out.println("filePath==" + map1.get("filePath"));
            System.out.println("fileName==" + map1.get("fileName"));
        }
    }

    public static Map textToSpeech(String text, String files) {

        File file = new File(files);
        //如果文件夹不存在则创建
        if (!file.exists() && !file.isDirectory()) {
            System.out.println("//不存在");
            file.mkdir();
        } else {
            System.out.println("//目录存在");
        }

        ActiveXComponent ax = null;
        try {
            ax = new ActiveXComponent("Sapi.SpVoice");
            //运行时输出语音内容
            Dispatch spVoice = ax.getObject();

            //只朗读不保存
            
            // 音量 0-100
            /*ax.setProperty("Volume", new Variant(100));
         // 语音朗读速度 -10 到 +10
         ax.setProperty("Rate", new Variant(0));
         // 执行朗读
         Dispatch.call(spVoice, "Speak", new Variant(text));*/

            //下面是构建文件流把生成语音文件

            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);
            //调用输出 文件流打开方法,创建一个.wav文件
            String fileName = new Date().getTime() + ".wav";
            String filePath = files + "\\" + fileName;
            Dispatch.call(spFileStream, "Open", new Variant(filePath), new Variant(3), new Variant(true));
            //设置声音对象的音频输出流为输出文件对象
            Dispatch.putRef(spVoice, "AudioOutputStream", spFileStream);
            //设置音量 0到100
            Dispatch.put(spVoice, "Volume", new Variant(100));
            //设置朗读速度
            Dispatch.put(spVoice, "Rate", new Variant(-1));
            //开始朗读
            Dispatch.call(spVoice, "Speak", new Variant(text));

            //关闭输出文件
            Dispatch.call(spFileStream, "Close");
            Dispatch.putRef(spVoice, "AudioOutputStream", null);

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

            Map map = new HashMap<>();
            map.put("filePath", filePath);
            map.put("fileName", fileName);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}

注意:此只适用于windows

使用的是微软自带的TTS,若电脑未安装或因安装的系统是精简版被阉割,可使用以下工具修复(注:因情况不同 不保证每个都可被修复)

Win7系统TTS修复-采用原生Win7提取绿色修复_2018-02-06.zip - 蓝奏云

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/924427.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-16
下一篇 2022-05-16

发表评论

登录后才能评论

评论列表(0条)

保存