java把文字转MP3语音案例

一 工具下载:
https://download.csdn.net/download/jinhuding/89723540

二代码

bash 复制代码
<dependency>
            <groupId>com.hynnet</groupId>
            <artifactId>jacob</artifactId>
            <version>1.18</version>
</dependency>
java 复制代码
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class ReadText {
    public static void main(String[] args) {
        text("D:\\a.mp3","你好啊",60,0);
    }

    /**
     * 文本转音频
     *
     * @param path   音频生成路径(全路径,带文件名)(例:D:\\aa\\a.mp3)
     * @param text   文本内容
     * @param volume 音量大小 0 - 100
     * @param speed  语音朗读速度 -10 到 +10
     * @return 是否成功
     */
    public static boolean text(String path, String text, int volume, int speed) {
        try {
            // 调用dll朗读方法
            ActiveXComponent ax = new ActiveXComponent("Sapi.SpVoice");
            // 音量 0 - 100
            ax.setProperty("Volume", new Variant(volume));
            // 语音朗读速度 -10 到 +10
            ax.setProperty("Rate", new Variant(speed));
            // 输入的语言内容
            Dispatch dispatch = ax.getObject();
            // 本地执行朗读
            Dispatch.call(dispatch, "Speak", new Variant(text));

            //开始生成语音文件,构建文件流
            ax = new ActiveXComponent("Sapi.SpFileStream");
            Dispatch sfFileStream = ax.getObject();
            //设置文件生成格式
            ax = new ActiveXComponent("Sapi.SpAudioFormat");
            Dispatch fileFormat = ax.getObject();

            // 设置音频流格式
            Dispatch.put(fileFormat, "Type", new Variant(22));
            // 设置文件输出流格式
            Dispatch.putRef(sfFileStream, "Format", fileFormat);
            // 调用输出文件流打开方法,创建一个音频文件
            Dispatch.call(sfFileStream, "Open", new Variant(path), new Variant(3), new Variant(true));
            // 设置声音对应输出流为输出文件对象
            Dispatch.putRef(dispatch, "AudioOutputStream", sfFileStream);
            // 设置音量
            Dispatch.put(dispatch, "Volume", new Variant(volume));
            // 设置速度
            Dispatch.put(dispatch, "Rate", new Variant(speed));
            // 执行朗读
            Dispatch.call(dispatch, "Speak", new Variant(text));
            // 关闭输出文件
            Dispatch.call(sfFileStream, "Close");
            Dispatch.putRef(dispatch, "AudioOutputStream", null);

            // 关闭资源
            sfFileStream.safeRelease();
            fileFormat.safeRelease();
            // 关闭朗读的操作
            dispatch.safeRelease();
            ax.safeRelease();
            return true;
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }


}
相关推荐
武子康1 小时前
Java-72 深入浅出 RPC Dubbo 上手 生产者模块详解
java·spring boot·分布式·后端·rpc·dubbo·nio
_殊途2 小时前
《Java HashMap底层原理全解析(源码+性能+面试)》
java·数据结构·算法
椰椰椰耶3 小时前
【Spring】拦截器详解
java·后端·spring
没有bug.的程序员3 小时前
JAVA面试宝典 - 《MyBatis 进阶:插件开发与二级缓存》
java·面试·mybatis
倔强青铜34 小时前
苦练Python第18天:Python异常处理锦囊
开发语言·python
u_topian4 小时前
【个人笔记】Qt使用的一些易错问题
开发语言·笔记·qt
没有羊的王K5 小时前
SSM框架学习——day1
java·学习
珊瑚里的鱼5 小时前
LeetCode 692题解 | 前K个高频单词
开发语言·c++·算法·leetcode·职场和发展·学习方法
又菜又爱coding5 小时前
安装Keycloak并启动服务(macOS)
java·keycloak
AI+程序员在路上5 小时前
QTextCodec的功能及其在Qt5及Qt6中的演变
开发语言·c++·qt