最近,遇到一个需求,将文字转化为语音并存储为文件。经过查阅大佬们的博客,采用jacob调用windows语音库合成的方式实现,后文中的代码为引用的大佬们的博客,没有具体记录采用的哪位大佬的,在此感谢各位。
写本博客的目的有二:
1、记录过程,后续参考;
2、寻找dll命令库文件比较麻烦,在此分享一下。
jacob1.18版本下载地址:https://github.com/freemansoft/jacob-project/releases/tag/Root_B-1_18
访问该地址后,拉到最下方,找到图中箭头的地方点击下载即可,由于访问github访问较慢,提供一下百度网盘的下载地址:
链接:https://pan.baidu.com/s/1dTJ4eJ8cajPr_EAhSkC9oQ
提取码:g4eu
下载后的压缩包解压缩后文件目录如图所示:
windows64位系统将上图中箭头标注的文件复制到jdk的bin目录下,32位系统复制x86版本即可,如下图所示:
以下是java实现demo:
pom文件中引入对应版本依赖:
<!-- https://mvnrepository.com/artifact/com.hynnet/jacob -->
<dependency>
<groupId>com.hynnet</groupId>
<artifactId>jacob</artifactId>
<version>1.18</version>
</dependency>
demo1:朗读文字
package com.im.imserver.jacob;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
public class TestJacob {
public static void main(String[] args) {
ActiveXComponent sap = new ActiveXComponent("Sapi.SpVoice");
// Dispatch是做什么的?
Dispatch sapo = sap.getObject();
try {
// 音量 0-100
sap.setProperty("Volume", new Variant(100));
// 语音朗读速度 -10 到 +10
sap.setProperty("Rate", new Variant(-2));
Variant defalutVoice = sap.getProperty("Voice");
Dispatch dispdefaultVoice = defalutVoice.toDispatch();
Variant allVoices = Dispatch.call(sapo, "GetVoices");
Dispatch dispVoices = allVoices.toDispatch();
Dispatch setvoice = Dispatch.call(dispVoices, "Item", new Variant(1)).toDispatch();
ActiveXComponent voiceActivex = new ActiveXComponent(dispdefaultVoice);
ActiveXComponent setvoiceActivex = new ActiveXComponent(setvoice);
Variant item = Dispatch.call(setvoiceActivex, "GetDescription");
// 执行朗读
Dispatch.call(sapo, "Speak", new Variant("转B15023,稍后到停车区检修"));
} catch (Exception e) {
e.printStackTrace();
} finally {
sapo.safeRelease();
sap.safeRelease();
}
}
}
demo2:将文字转为音频文件:
package com.im.imserver.jacob;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
public class Test2 {
public static void main(String[] args) {
ActiveXComponent ax = null;
try {
ax = new ActiveXComponent("Sapi.SpVoice");
Dispatch spVoice = ax.getObject();
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文件
Dispatch.call(spFileStream, "Open", new Variant("F:\\test.wav"), new Variant(3), new Variant(true));
//设置声音对象的音频输出流为输出文件对象
Dispatch.putRef(spVoice, "AudioOutputStream", spFileStream);
//设置音量 0到100
Dispatch.put(spVoice, "Volume", new Variant(100));
//设置朗读速度
Dispatch.put(spVoice, "Rate", new Variant(-2));
//设置语音库
// Dispatch.put(spVoice, "Voice", 1);
//开始朗读
Dispatch.call(spVoice, "Speak", new Variant("转B15023,稍后到停车区检修"));
//关闭输出文件
Dispatch.call(spFileStream, "Close");
Dispatch.putRef(spVoice, "AudioOutputStream", null);
spAudioFormat.safeRelease();
spFileStream.safeRelease();
spVoice.safeRelease();
ax.safeRelease();
} catch (Exception e) {
e.printStackTrace();
}
}
}