本文主要是介绍java把文字转MP3语音案例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一 工具下载:
https://download.csdn.net/download/jinhuding/89723540
二代码
<dependency><groupId>com.hynnet</groupId><artifactId>jacob</artifactId><version>1.18</version>
</dependency>
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 - 100ax.setProperty("Volume", new Variant(volume));// 语音朗读速度 -10 到 +10ax.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;}}
这篇关于java把文字转MP3语音案例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!