注意: 只能在windows上使用!!!

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

/**
 * 文字转语音
 * jdk bin文件中需要导入jacob-1.19-x64.dll
 */
public class JacobTTS {

    public static void textToSpeech(String text) {
        // 创建与微软应用程序的新连接.传入的参数是注册表中注册的程序的名称
        ActiveXComponent sap = new ActiveXComponent("Sapi.SpVoice");
        // 获取执行对象
        Dispatch sapo = sap.getObject();

        try {
            // 音量 0-100
            sap.setProperty("Volume", new Variant(100));
            // 语音朗读速度 -10 到 +10
            sap.setProperty("Rate", new Variant(-2));
            // 执行朗读
            Dispatch.call(sapo, "Speak", new Variant(text));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭执行对象
            sapo.safeRelease();
            // 关闭连接
            sap.safeRelease();
        }
    }

    public static void main(String[] args) {
        textToSpeech("hi, 我叫张三.");

        /**
         * 需要将jacob-1.19-x64.dll导入到下列文件夹中的任意一个里面
         */
        String property = System.getProperty("java.library.path");
        String replaceAll = property.replaceAll(";", ";\n");
        System.out.println(replaceAll);
    }

}

jacob-1.19-x64.dll下载地址:

链接:https://pan.baidu.com/s/1GFIwrtaJ3gXwo0-ViO71Gg 
提取码:2333 
复制这段内容后打开百度网盘手机App,操作更方便哦

注意: 只能在windows上使用!!!