jacob 进行文字转语音,并输出语音
参考:https://www.meihaocloud.com/1060.html

package test1; import com.jacob.activeX.ActiveXComponent; import com.jacob.com.Dispatch; import com.jacob.com.Variant; public class JacobTest { /** * 将文字直接朗读 * @param text */ public static void textToSpeech(String text) { 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(2)); // 执行朗读 Dispatch.call(spVoice, "Speak", new Variant(text)); spVoice.safeRelease(); ax.safeRelease(); } catch (Exception e) { e.printStackTrace(); } } /** * 将文字保存为音频文件 wav、mp3 * @param text * @param filePath */ public static void textToAudio(String text, String filePath) { 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(2)); // 将文件流把生成音频文件 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 .mp3文件 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)); // 设置朗读速度 -10 到 +10 Dispatch.put(spVoice, "Rate", new Variant(2)); // 开始朗读 Dispatch.call(spVoice, "Speak", new Variant(text)); // 关闭输出文件 Dispatch.call(spFileStream, "Close"); Dispatch.putRef(spVoice, "AudioOutputStream", null); spAudioFormat.safeRelease(); spFileStream.safeRelease(); spVoice.safeRelease(); ax.safeRelease(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { String str = "12日公布的“十四五”规划和2035年远景目标纲要明确提出,按照“小步调整、弹性实施、分类推进、统筹兼顾”等原则,逐步延迟法定退休年龄。"; // 直接朗读 textToSpeech(str); // 生成文件 textToAudio(str, "d://test.mp3"); } }
下载:https://github.com/freemansoft/jacob-project/releases
报错:no jacob-1.20-x64 in java.library.path
将jacob-1.20-x64.dll或者jacob-1.20-x86.dll放到 /jre/bin下
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)