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;
    }


}
相关推荐
爬虫程序猿22 分钟前
如何利用 Java 爬虫根据 ID 获取某手商品详情:实战指南
java·开发语言·爬虫
大模型铲屎官28 分钟前
【Python-Day 16】代码复用基石:详解 Python 函数的定义与调用
开发语言·人工智能·pytorch·python·深度学习·大模型·函数
SSSYUsssyu35 分钟前
Java NIO(New I/O)
java·开发语言·nio
从零开始学习人工智能36 分钟前
深入解析异步编程:Java NIO、Python `async/await` 与 C# `async/await` 的对比
java·python·nio
TNTLWT1 小时前
Qt功能区:Ribbon控件
开发语言·qt
暮乘白帝过重山1 小时前
JVM之GC常见的垃圾回收器
java·jvm·算法
我最厉害。,。1 小时前
JS 应用&安全案例&泄漏云配置&接口调试&代码逻辑&框架漏洞自检
开发语言·javascript·安全
林的快手2 小时前
基于 Redis 实现短信验证码登录功能的完整方案
java·开发语言·数据库·redis·缓存·bootstrap
看到我,请让我去学习2 小时前
C语言—Linux环境下CMake设置库(动态/静态)
linux·服务器·c语言·开发语言·数据结构
daiwoliyunshang2 小时前
C++ 11(1):
开发语言·c++