java 语音合成并生成wav文件

岁酱吖の 提交于 2019-12-24 16:40:13

1.下载jacob文件,根据自己操作系统,将jacob-1.18-x64.dll文件复制到C:/windows/System32文件夹中

下载链接: https://pan.baidu.com/s/1mimoaBeFhzVMsGOLzsvpBw 提取码: wsfz

2.pom.xml引入依赖

<!--文字转语音-->
<dependency>
      <groupId>com.hynnet</groupId>
      <artifactId>jacob</artifactId>
      <version>1.18</version>
</dependency>

3.代码

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

public class ReadText {
    public static void main(String[] args) {
//        ActiveXComponent sap = new ActiveXComponent("Sapi.SpVoice");
////        // Dispatch是做什么的?
//        Dispatch sapo = sap.getObject();
        try {

//            // 音量 0-100
//            sap.setProperty("Volume", new Variant(100));
//            // 语音朗读速度 -10 到 +10
//            sap.setProperty("Rate", new Variant(0));
//
//            Variant defalutVoice = sap.getProperty("Voice");
//
//            Dispatch dispdefaultVoice = defalutVoice.toDispatch();
//            Variant allVoices = Dispatch.call(sapo, "GetVoices");
//            Dispatch dispVoices = allVoices.toDispatch();
//
//            Dispatch setvoice = Dispatch.call(dispVoices, "Item", new Variant(1)).toDispatch();
//            ActiveXComponent setvoiceActivex = new ActiveXComponent(setvoice);
//
//            Variant item = Dispatch.call(setvoiceActivex, "GetDescription");
//            // 执行朗读
//            Dispatch.call(sapo, "Speak", new Variant("你好!"));


            //朗读
            ActiveXComponent sap = new ActiveXComponent("Sapi.SpVoice");
////        // Dispatch是做什么的?
            Dispatch sapo = sap.getObject();

            Dispatch.call(sapo, "Speak", new Variant("你好!"));

 //         下面是构建文件流把生成语音文件
            sap = new ActiveXComponent("Sapi.SpFileStream");
            Dispatch spFileStream = sap.getObject();

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

            // 设置音频流格式
            Dispatch.put(spAudioFormat, "Type", new Variant(22));
            // 设置文件输出流格式
            Dispatch.putRef(spFileStream, "Format", spAudioFormat);
            // 调用输出 文件流打开方法,创建一个.wav文件
            Dispatch.call(spFileStream, "Open", new Variant("./text.wav"), new Variant(3), new Variant(true));
            // 设置声音对象的音频输出流为输出文件对象
            Dispatch.putRef(sapo, "AudioOutputStream", spFileStream);
            // 设置音量 0到100
            Dispatch.put(sapo, "Volume", new Variant(100));
            // 设置朗读速度
            Dispatch.put(sapo, "Rate", new Variant(-2));
            // 开始朗读
            Dispatch.call(sapo, "Speak", new Variant("你好,欢迎您"));

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

            spAudioFormat.safeRelease();
            spFileStream.safeRelease();
            sapo.safeRelease();
            sap.safeRelease();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
//            sapo.safeRelease();
//            sap.safeRelease();
        }
    }

}

参考地址:https://www.cnblogs.com/coder-wzr/p/11321754.html

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!