提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
前言
遇到一个需求,当界面刚加载时,播报异常报警信息。记录一下SpringBoot+vue2语音播报功能的实现。
环境需要的一种语言文件:jacob-1.18-x64.dll文件:链接:https://pan.baidu.com/s/1ET0pmOYyVQDubWfWM2_GbQ
提取码:ae86
一、jacob是什么?
Jacob 是Java-COM Bridge的缩写,它在Java与微软的COM组件之间构建一座桥梁。使用Jacob自带的DLL动态链接库,并通过JNI的方式实现了在Java平台上对COM程序的调用。
二、使用步骤
1.引入pom.xml文件
代码如下(示例):
<!-- 文字转语音 -->
<dependency>
<groupId>com.hynnet</groupId>
<artifactId>jacob</artifactId>
<version>1.18</version>
</dependency>
2.将jacob-1.18-x64.dll放到jdk的bin目录下
如下图:
3.本地播放
代码如下(示例):
/**【语音播报方法】**/
public static boolean speakingText(String readText){
boolean isFinish = true;
ActiveXComponent sap = new ActiveXComponent("Sapi.SpVoice");
try {
sap.setProperty("Volume",new Variant(100)); // 音量 0-100
sap.setProperty("Rate",new Variant(-1)); // 语音朗读速度 -10 到 +10
sap.invoke("Speak", new Variant(readText));
Dispatch sapo = sap.getObject(); // 获取执行对象
Dispatch.call(sapo,"Speak",new Variant(readText)); // 执行朗读
sapo.safeRelease(); // 关闭执行对象
}catch (Exception e){
isFinish = false;
e.printStackTrace();
}finally {
sap.safeRelease(); // 关闭执行对象
}
return isFinish;
}
这个是本地播放,参数为你想要播放的那句话。
4.如果你想将文字转换成的录音文件进行保存
代码如下(示例):
/**
* 生成语音文件
*/
public static void audioFile(String text){
try {
//jacob.dll没成功安装,执行这一步会出错
//构建音频格式 调用注册表应用
Dispatch spAudioFormat = new ActiveXComponent("Sapi.SpAudioFormat").getObject();
//音频文件输出流
Dispatch spFileStream = new ActiveXComponent("Sapi.SpFileStream").getObject();
//构建音频对象
Dispatch spVoice = new ActiveXComponent("Sapi.SpVoice").getObject();
//设置spAudioFormat音频流格式类型22
Dispatch.put(spAudioFormat, "Type", new Variant(22));
//设置spFileStream文件输出流的音频格式
Dispatch.putRef(spFileStream, "Format", spAudioFormat);
//设置spFileStream文件输出流参数地址等
Dispatch.call(spFileStream, "Open", new Variant("D:\\1\\48641486.wav"), new Variant(3), new Variant(true));
//设置spVoice声音对象的音频输出流为输出文件对象
Dispatch.putRef(spVoice, "AudioOutputStream", spFileStream);
//设置spVoice声音对象的音量大小100
Dispatch.put(spVoice, "Volume", new Variant(100));
//设置spVoice声音对象的速度 0为正常速度,范围【..-2 -1 0 1 2..】
Dispatch.put(spVoice, "Rate", new Variant(0));
//设置spVoice声音对象中的文本内容
Dispatch.call(spVoice, "Speak", new Variant(text));
//关闭spFileStream输出文件
Dispatch.call(spFileStream, "Close");
//释放资源
spVoice.safeRelease();
spAudioFormat.safeRelease();
spFileStream.safeRelease();
}catch (Exception e){
System.out.println(e.getMessage());
}
}
//设置spFileStream文件输出流参数地址等
Dispatch.call(spFileStream, “Open”, new Variant(“D:\1\48641486.wav”), new Variant(3), new Variant(true));
该处使用的D:\1\48641486.wav就是你要保存的地址。
5.如果你想将文件转换成流的方式并且被web端解析可以采用下面的方式
java接口:
@GetMapping("/speech")
public static ResponseEntity<byte[]> downWav() throws IOException {
// 调用生成 WAV 文件的方法,假设文件保存在 D:\\1\\48641486.wav
String filePath = "D:\\1\\48641486.wav";
Path path = Paths.get(filePath);
// 读取文件内容为字节数组
byte[] bytes = Files.readAllBytes(path);
// 设置响应头,告诉浏览器以音频格式播放文件
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.parseMediaType("audio/wav"));
// 如果想要下载解开一下注释 sample.wav是文件名称
// headers.setContentDisposition(ContentDisposition.builder("attachment").filename("sample.wav").build());
// 返回 WAV 文件的 ResponseEntity
return ResponseEntity.ok()
.headers(headers)
.body(bytes);
}
vue前端:
<div>
<audio ref="audioPlayer" controls autoplay style="display: none;"></audio>
</div>
mounted() {
this.playAudio();
},
methods: {
playAudio() {
// http://localhost:8080/textToSpeechController/speech是请求接口,也可以去调用接口的js中去写。
fetch('http://localhost:8080/textToSpeechController/speech')
.then(response => response.arrayBuffer())
.then(audioData => {
const blob = new Blob([audioData], { type: 'audio/wav' });
const audioUrl = URL.createObjectURL(blob);
this.$refs.audioPlayer.src = audioUrl;
})
.catch(error => {
console.error('Failed to fetch audio:', error);
});
}
}
三、总结
抛砖引玉记录自己的解决过的问题,如有见解评论区讨论。