使用java实现语音朗读

jacob jar包及API下载:https://download.csdn.net/download/hjinping/10391308


1、64位操作系统的将jacob-1.17-M2-x64.dll添加到JDK的bin目录和Windows的system32目录(32位选择对应的dll文件,加入到对应目录下面

2、将jar包加入到项目中

3、编码实现:

demo:

package com.jeeplus.common.utils;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;


import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;


public class TalkUtil {
public static void main(String[] args) throws Exception {
talkString("你好,很高兴认识你。");
// talkText("C:/aa.txt");


}


public static void talkString(String talk) {
ActiveXComponent sap = new ActiveXComponent("Sapi.SpVoice");
try {
// 音量 0-100
sap.setProperty("Volume", new Variant(100));
// 语音朗读速度 -10 到 +10
sap.setProperty("Rate", new Variant(-2));
// 获取执行对象
Dispatch sapo = sap.getObject();
// 执行朗读
Dispatch.call(sapo, "Speak", new Variant(talk));
// 关闭执行对象
sapo.safeRelease();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭应用程序连接
sap.safeRelease();
}
}


public static void talkText(String path) throws Exception {
ActiveXComponent sap = new ActiveXComponent("Sapi.SpVoice");
// 输入文件
File srcFile = new File(path);
// 使用包装字符流读取文件
BufferedReader br = new BufferedReader(new FileReader(srcFile));
String content = br.readLine();
try {
// 音量 0-100
sap.setProperty("Volume", new Variant(100));
// 语音朗读速度 -10 到 +10
sap.setProperty("Rate", new Variant(-1));
// 获取执行对象
Dispatch sapo = sap.getObject();
// 执行朗读
while (content != null) {
Dispatch.call(sapo, "Speak", new Variant(content));
content = br.readLine();
}
// 关闭执行对象
sapo.safeRelease();
} catch (Exception e) {
e.printStackTrace();
} finally {
br.close();
// 关闭应用程序连接
sap.safeRelease();
}
}


}



猜你喜欢

转载自blog.csdn.net/hjinping/article/details/80191658