1、将动态库放在resource文件目录下
2、编写相关加载逻辑
import lombok.extern.slf4j.Slf4j;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.HashMap;
/**
* @Description: 加载动态库 .dll文件
* @author: Be.insighted
* @create: 2024/4/15 8:51
* @since 1.0.0
*/
@Slf4j
public class LoadLibraryUtil {
public static void addLibrary() {
File file = new File("dll");
String absolutePath = file.getAbsolutePath();
log.info("add lib:{}",absolutePath);
if (!file.exists()) {
file.mkdirs();
}
try {
addLibDir(absolutePath);
} catch (IOException e1) {
e1.printStackTrace();
}
}
public static void addLibrary(String path) {
File file = new File(path);
String absolutePath = file.getAbsolutePath();
log.info("add lib path from:{}",absolutePath);
if (!file.exists()) {
file.mkdirs();
}
try {
addLibDir(absolutePath);
} catch (IOException e1) {
e1.printStackTrace();
}
}
public static void addLibDir(String s) throws IOException {
HashMap<String, String> stringStringHashMap = new HashMap<>();
stringStringHashMap.put(null, null);
try {
Field field = ClassLoader.class.getDeclaredField("usr_paths");
field.setAccessible(true);
String[] paths = (String[]) field.get(null);
for (int i = 0; i < paths.length; i++) {
if (s.equals(paths[i])) {
return;
}
}
String[] tmp = new String[paths.length + 1];
System.arraycopy(paths, 0, tmp, 0, paths.length);
tmp[paths.length] = s;
field.set(null, tmp);
} catch (IllegalAccessException e) {
throw new IOException("Failed to get permissions to set library path");
} catch (NoSuchFieldException e) {
throw new IOException("Failed to get field handle to set library path");
}
}
}
3、测试
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.zhuao.vo.TTSVo;
import com.jacob.com.Variant;
import lombok.extern.slf4j.Slf4j;
import java.io.File;
/**
* @Description: 使用加载的dll进行相关测试
* @author: Be.insighted
* @create: 2024/8/8 8:51
* @since 1.0.0
*/
@Slf4j
public class MsTtsUtil {
public static void main(String[] args) {
LoadLibraryUtil.addLibrary();
String parentPath = "D:/dev_workspace/java/litong-project/litongjava-windows-tts/target/classes/litongjava-windows-tts/file/auido";
File file = new File(parentPath);
if (!file.exists()) {
file.mkdirs();
}
TTSVo ttsVo = new TTSVo();
ttsVo.setType(6);
ttsVo.setVolume(100);
ttsVo.setRate(0);
ttsVo.setText("今天猪肉又涨价了,一下子涨了两块钱");
ttsToFile(ttsVo, parentPath);
ttsVo.setText("今天羊肉羊肉也涨价了");
ttsToFile(ttsVo, parentPath);
// String path = "text\\过秦论.txt";
// textToFile(path);
}
/**
* 获取当前发音人
*/
public static String getCurrentVoice() {
// 创建与微软应用程序的新连接。传入的参数是注册表中注册的程序的名称。相当实例化Sapi.SpVoice对象,但是ax并不是Sapi.SpVoice,ax是包装信息,ax包含Sapi.SpVoice
ActiveXComponent ax = new ActiveXComponent("Sapi.SpVoice");
String currentVoice = getCurrentVoice(ax);
ax.safeRelease();
return currentVoice;
}
private static String getCurrentVoice(ActiveXComponent ax) {
// 获取Sapi.SpVoice对象的voice属性值,voice的属性值是一个voice对象,对象使用Dispatch包装
Dispatch dispatch = ax.getProperty("voice").toDispatch();
// 执行voice对象的GetDescription方法,返回值可能使用String,使用Variant包装
Variant description = Dispatch.call(dispatch, "GetDescription");
return description.toString();
}
/**
* 语音转文字音频
*
* @param ttsVo
*/
public static String ttsToFile(TTSVo ttsVo, String parentPath) {
ActiveXComponent ax = new ActiveXComponent("Sapi.SpVoice");
String filePath = getFilePath(ax, ttsVo, parentPath);
try {
// 运行时输出语音内容
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", ttsVo.getType());
// 设置文件输出流格式
Dispatch.putRef(spFileStream, "Format", spAudioFormat);
// 调用输出 文件流打开方法,创建一个.wav文件
Dispatch.call(spFileStream, "Open", new Variant(filePath), new Variant(3), new Variant(true));
// 设置声音对象的音频输出流为输出文件对象
Dispatch.putRef(spVoice, "AudioOutputStream", spFileStream);
// 设置音量 0到100
Dispatch.put(spVoice, "Volume", new Variant(ttsVo.getVolume()));
// 设置朗读速度
Dispatch.put(spVoice, "Rate", new Variant(ttsVo.getRate()));
// 开始朗读,实际上是合成得到文件
Dispatch.call(spVoice, "Speak", new Variant(ttsVo.getText()));
// 关闭输出文件
Dispatch.call(spFileStream, "Close");
Dispatch.putRef(spVoice, "AudioOutputStream", null);
spAudioFormat.safeRelease();
spFileStream.safeRelease();
spVoice.safeRelease();
} catch (Exception e) {
e.printStackTrace();
} finally {
ax.safeRelease();
}
return filePath;
}
public static String getFilePath(ActiveXComponent ax, TTSVo ttsVo, String parentPath) {
// 文件名规则
// voice_type_volume_rate_md5
String voice = null;
if (ttsVo.getVoice() == null) {
voice = getCurrentVoice(ax);
} else {
voice = ttsVo.getVoice();
}
String filename = append("_", ttsVo.getType(), ttsVo.getVolume(), ttsVo.getRate());
// 添加文件名称
filename += "_" + HashUtil.md5(ttsVo.getText());
// 添加文件后缀
if (ttsVo.getType() == 6) {
filename += ".wav";
}
// 发音人和加目录
filename = parentPath + File.separator + voice + "_" + filename;
return filename;
}
private static String append(String string, int... ints) {
String retval = new String();
for (int i : ints) {
retval += "_" + i;
}
return retval;
}
}