avatar

Java将字符串输出成音频的工具类

导入配置文件

下载dll配置文件

1
2
链接:https://pan.baidu.com/s/1fqdgABV8-musViEBKMgVeg 
提取码:2559

将压缩包中的配置文件移动到JDK的bin目录下

bin目录

编写Util工具类

引入jacob依赖

1
2
3
4
5
<dependency>
<groupId>com.hynnet</groupId>
<artifactId>jacob</artifactId>
<version>1.18</version>
</dependency>

编写工具类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import org.slf4j.Logger;
import com.jacob.com.Variant;
import com.jacob.com.Dispatch;
import org.slf4j.LoggerFactory;
import com.jacob.activeX.ActiveXComponent;

/** 音频操作 */
public class AudioFrequencyOperating {

/** 日志打印 */
private static final Logger log = LoggerFactory.getLogger(StringOperatingUtil.class);

/**
* 播放自定义字符串
* @param volume 音量,允许范围1~100之间
* @param rate 朗读速度,允许范围-10~10之间
* @param text 要输出的字符串
* @param whetherGenerateFile 是否生成文件
*/
public static void textToSpeech(Integer volume, Integer rate, String text, Boolean whetherGenerateFile) {

if (volume == null || !(0 < volume && volume <= 100)) throw new RuntimeException("音量输出超出范围");
if (rate == null || !(-10 <= rate && rate <= 10)) throw new RuntimeException("朗读速度超出范围");

try {

ActiveXComponent activeXComponent = new ActiveXComponent("Sapi.SpVoice");
/* 运行时输出语音内容 */
Dispatch dispatch = activeXComponent.getObject();
/* 音量 0-100 */
activeXComponent.setProperty("Volume", new Variant(volume));
/* 语音朗读速度 -10 到 +10 */
activeXComponent.setProperty("Rate", new Variant(rate));
/* 执行朗读 */
Dispatch.call(dispatch, "Speak", new Variant(text));

log.info("音频输出成功");

/* 是否将构建的文件流生成语音文件 */
if (whetherGenerateFile) {
activeXComponent = new ActiveXComponent("Sapi.SpFileStream");
Dispatch spFileStream = activeXComponent.getObject();

activeXComponent = new ActiveXComponent("Sapi.SpAudioFormat");
Dispatch spAudioFormat = activeXComponent.getObject();

/* 设置音频流格式 */
Dispatch.put(spAudioFormat, "Type", new Variant(22));
/* 设置文件输出流格式 */
Dispatch.putRef(spFileStream, "Format", spAudioFormat);
/* 调用输出 文件流打开方法,创建一个.mp3文件 */
Dispatch.call(spFileStream, "Open", new Variant("./text.mp3"), new Variant(3), new Variant(true));
/* 设置声音对象的音频输出流为输出文件对象 */
Dispatch.putRef(dispatch, "AudioOutputStream", spFileStream);
/* 设置音量0到100 */
Dispatch.put(dispatch, "Volume", new Variant(100));
/* 设置朗读速度 */
Dispatch.put(dispatch, "Rate", new Variant(0));
/* 开始朗读 */
Dispatch.call(dispatch, "Speak", new Variant(text));
/* 关闭输出文件 */
Dispatch.call(spFileStream, "Close");
Dispatch.putRef(dispatch, "AudioOutputStream", null);

log.info("音频文件已生成到项目的根目录下");

spAudioFormat.safeRelease();
spFileStream.safeRelease();

}

dispatch.safeRelease();
activeXComponent.safeRelease();

} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
文章作者: 123
文章链接: https://gao5805123.github.io/123/2020/11/12/Java%E5%B0%86%E5%AD%97%E7%AC%A6%E4%B8%B2%E8%BE%93%E5%87%BA%E6%88%90%E9%9F%B3%E9%A2%91%E7%9A%84%E5%B7%A5%E5%85%B7%E7%B1%BB/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 123
打赏
  • 微信
    微信
  • 支付宝
    支付宝