标签
工具
字数
578 字
阅读时间
3 分钟
集成步骤
调用windows的tts进行语音播报,win10可直接调用,win7可能需要修复tts
方式,将需要播放的内容放入队列中,开启一个线程循环获取队列中参数,存在则进行播放
需要将javacob放入%JAVA_HOME%/bin/目录下
播放服务
java
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.commnetsoft.pub.util.common.StringUtil;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
/**
* Windows TTS服务
*
* @author Brack.zhu
* @date 2020年12月16日
*/
public class WindowsTTSservice {
/**
* 音量 0-100
*/
private int volume = 100;
/**
* 语音朗读速度 -10 到 +10
*/
private int rate = -2;
private Logger log = LoggerFactory.getLogger(getClass());
/**
* 语音转文字并播放
*
* @param txt
*/
public void textToSpeech(String text) {
if (StringUtil.isEmpty(text)) {
return;
}
ActiveXComponent ax = null;
try {
ax = new ActiveXComponent("Sapi.SpVoice");
// 运行时输出语音内容
Dispatch spVoice = ax.getObject();
ax.setProperty("Volume", new Variant(volume));
ax.setProperty("Rate", new Variant(rate));
// 执行朗读
Dispatch.call(spVoice, "Speak", new Variant(text));
spVoice.safeRelease();
} catch (Exception e) {
log.error("语音播放失败,{}", text, e);
}finally {
ax.safeRelease();
}
}
public int getVolume() {
return volume;
}
public void setVolume(int volume) {
if (0 <= volume && volume <= 100) {
log.warn("语音播放音量设置为:{}-->{}", this.volume, volume);
this.volume = volume;
}
}
public int getRate() {
return rate;
}
public void setRate(int rate) {
if(-10 <= rate && rate <= 10) {
log.warn("语音朗读速度设置为:{}-->{}", this.rate, rate);
this.rate = rate;
}
}
}复制dll、队列、从队列中获取消息
java
public boolean copyJacobDll() {
File[] dllFiles = new File(Utility.getUserDir() + Utility.getFileSeparator()+"/jni/jacob/tts").listFiles((File dir, String name) -> {
if (name.contains("dll")) {
return true;
}
return false;
});
String javaHome=System.getProperty("java.home");
for(File file:dllFiles) {
String fileName=file.getName();
String tagerPath=javaHome+Utility.getFileSeparator()+"bin"+Utility.getFileSeparator()+fileName;
try {
Files.copy(file, new File(tagerPath));
} catch (IOException e) {
log.error("dll文件拷贝异常:", e);
return false;
}
}
return true;
}
private LinkedBlockingQueue<String> speechTexts = new LinkedBlockingQueue<>();
/**
* 检索并删除此队列的头,如有必要,等待元素可用。 队列无数据一直同步等待。
*
* @return
* @throws InterruptedException
*/
public String takeSpeechText() throws InterruptedException {
return speechTexts.take();
}线程
java
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.commnetsoft.pub.pro.base.module.ModuleManager;
/**
* TTS文本转语音线程
*
* @author Brack.zhu
* @date 2020年12月16日
*/
public class TtsTextSpeechThread extends Thread {
private boolean stopFlag = false;
private Logger log = LoggerFactory.getLogger(getClass());
public TtsTextSpeechThread() {
super("Tts Text Speech Thread");
}
public void callStop() {
log.warn("TtsTextSpeechThread(TTS文本转语音线程)触发停止!");
stopFlag = true;
this.interrupt();
}
@Override
public void run() {
log.warn("TtsTextSpeechThread(TTS文本转语音线程)启动!");
VoiceModule voiceModule = ModuleManager.getInstance().get(VoiceModule.class);
while (!stopFlag) {
try {
String text = voiceModule.takeSpeechText();
voiceModule.callTTSspeechText(text);
} catch (InterruptedException e) {
// ignore
}
}
log.warn("TtsTextSpeechThread(TTS文本转语音线程)停止!");
}
}