java 文字转语音
安装maven包 调用问题:1、如果出现项目启动报错 no jacob-1.18-x64 in java.library.path的问题是jdk中缺少dll方法需要下载一份到jdk的bin目录下 。 下载包2、如果执行朗读,默认调用系统中的TTS语音播放引擎,若本机没有语音设备可能会抛出异常(com.jacob.com.ComFailException: Invoke of: Speak)可参考:
·
使用 jacob-project 插件
注意:
jacob不支持linux;jacob是“JAVA-COM Bridge”的缩写,是一个中间件,能够提供自动化访问MS系统下COM组件和“Win32 libraries”的功能,并且jacob只支持windows,不支持linux
安装maven包
<dependency> <groupId>com.hynnet</groupId> <artifactId>jacob</artifactId> <version>1.18</version> </dependency>
调用
/**
* 路径可自己配置
* @param text 文本内容
* @param value 音量
* @param volueRow 读取速度
* @return 是否成功
*/
public static boolean text(String text, int value,int volueRow){
try {
//音频生成路径
String path = "E:\tmp";
//固定音量大小
if (value <0 || value >100){
value = 100;
}
//调用dll朗读方法
ActiveXComponent ax = new ActiveXComponent( "Sapi.SpVoice" ) ;
//音量 0-100
ax.setProperty("Volume",new Variant(value));
//朗读速度
ax.setProperty("Rate",new Variant(volueRow));
//输入的语言内容
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);
//调用输出文件流打开方法,创建一个.wav文件
Dispatch.call(sfFileStream,"Open",new Variant("E:\\tmp\\a.wav"),new Variant(3),new Variant(true));
//设置声音对应输出流为输出文件对象
Dispatch.putRef(dispatch,"AudioOutputStream",sfFileStream);
//设置音量
Dispatch.put(dispatch,"Volume",new Variant(value));
//设置速度
Dispatch.put(dispatch,"Rate",new Variant(volueRow));
//执行朗读
Dispatch.call(dispatch,"Speak",new Variant(text));
//关闭输出文件
Dispatch.call(sfFileStream,"Close");
Dispatch.putRef(dispatch,"AudioOutputStream",null);
//close
sfFileStream.safeRelease();
fileFormat.safeRelease();
//关闭朗读的操作
dispatch.safeRelease();
ax.safeRelease();
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
问题:
1、如果出现项目启动报错 no jacob-1.18-x64 in java.library.path的问题是jdk中缺少dll方法需要下载一份到jdk的bin目录下 。 下载包
2、如果执行朗读,默认调用系统中的TTS语音播放引擎,若本机没有语音设备
可能会抛出异常(com.jacob.com.ComFailException: Invoke of: Speak)
可参考:
更多推荐
所有评论(0)