一、完整资源直接看这里:
java调用window操作系统文本转语音并生成播放文件资源-CSDN文库
二、所需材料
材料一:最关键的,需要引用jacob包:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>wenzizhuanyy</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.hynnet</groupId>
<artifactId>jacob</artifactId>
<version>1.18</version>
</dependency>
</dependencies>
</project>
材料二:最好是将jacob的ddl放到jdk的bin下,虽然我也不知道这个是不是必须的:
三、代码
package com.lxp.common.utils;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComFailException;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
/**
* @Author: lxp
* @Date: 2024年3月21日10:12:50
*/
public class VoicePlayUtil {
public static void main(String[] args) throws IOException {
String content = readFile("E:\\【职业生涯】\\《锁定高端客户》\\文字版.txt");
textToSpeech(content);
}
/**
* 根据文本文件位置读取到一个string变量中
* @param filePath
* @return
*/
private static String readFile(String filePath) {
String content = "";
try {
File file = new File(filePath);
FileInputStream fis = new FileInputStream(file);
Scanner scanner = new Scanner(fis);
while (scanner.hasNextLine()) {
content += scanner.nextLine();
}
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return content;
}
/**
* 语音转文字并播放
*
* @param text 要播放的文字
* @throws Exception 异常捕捉
*/
public static void textToSpeech(String text) {
ActiveXComponent ax = null;
try {
ax = new ActiveXComponent("Sapi.SpVoice");
// 运行时输出语音内容
Dispatch spVoice = ax.getObject();
// 音量 0-100
ax.setProperty("Volume", new Variant(100));
// 语音朗读速度 -10 到 +10(-10语速最慢,10语速最快)
ax.setProperty("Rate", new Variant(1));
/* 执行朗读,默认调用系统中的TTS语音播放引擎,若本机没有语音设备
可能会抛出异常(com.jacob.com.ComFailException: Invoke of: Speak) */
System.out.println("开始语音播报");
Dispatch.call(spVoice, "Speak", new Variant(text));
System.out.println("语音播报完成");
// 下面是构建文件流生成语音文件
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("./锁定高端客户.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(1));
// 开始朗读
Dispatch.call(spVoice, "Speak", new Variant(text));
// 关闭输出文件
Dispatch.call(spFileStream, "Close");
Dispatch.putRef(spVoice, "AudioOutputStream", null);
spAudioFormat.safeRelease();
spFileStream.safeRelease();
spVoice.safeRelease();
ax.safeRelease();
} catch (ComFailException e) {
e.printStackTrace();
System.out.println("没有可用的音频,请连接外接设备(耳机或音箱播放)");
} catch (Exception e) {
e.printStackTrace();
System.out.println("语音播放错误:" + e.getMessage());
}
}
}