删除 src/main/java/com/bonus/autoweb/TestMain.java
This commit is contained in:
parent
673428752f
commit
5581bb6dbe
|
|
@ -1,565 +0,0 @@
|
||||||
package com.bonus.autoweb;
|
|
||||||
|
|
||||||
import com.bonus.autoweb.UI.entity.WeatherData;
|
|
||||||
import com.bonus.autoweb.base.DataConfig;
|
|
||||||
import com.bonus.autoweb.task.AutoWebTask;
|
|
||||||
import com.google.gson.Gson;
|
|
||||||
import com.jacob.activeX.ActiveXComponent;
|
|
||||||
import com.jacob.com.ComFailException;
|
|
||||||
import com.jacob.com.Dispatch;
|
|
||||||
import com.jacob.com.Variant;
|
|
||||||
import lombok.SneakyThrows;
|
|
||||||
import okhttp3.OkHttpClient;
|
|
||||||
import okhttp3.Request;
|
|
||||||
import okhttp3.Response;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import javax.sound.sampled.*;
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.text.ParseException;
|
|
||||||
import java.util.*;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 系统测试
|
|
||||||
* @author 24526
|
|
||||||
*/
|
|
||||||
public class TestMain {
|
|
||||||
private static Logger log = LoggerFactory.getLogger(TestMain.class);
|
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
|
||||||
log.info("执行任务开始。。。。。。");
|
|
||||||
// testRCCL();
|
|
||||||
// testqd();
|
|
||||||
// testjjb();
|
|
||||||
// testGetData();
|
|
||||||
// testDailrb();
|
|
||||||
// start("工作人员请注意,桌号8001顾客正在寻求帮助!!");
|
|
||||||
// testLog();
|
|
||||||
// testWeather();
|
|
||||||
new Thread(new Runnable() {
|
|
||||||
@SneakyThrows
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
try {
|
|
||||||
new TestMain().autoJob();
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
log.error("错误信息", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}).start();
|
|
||||||
}
|
|
||||||
|
|
||||||
//签到签退测试
|
|
||||||
private static void testWeather() throws InterruptedException, IOException {
|
|
||||||
OkHttpClient client = new OkHttpClient().newBuilder()
|
|
||||||
.connectTimeout(180, TimeUnit.SECONDS)
|
|
||||||
.readTimeout(180, TimeUnit.SECONDS)
|
|
||||||
.writeTimeout(180, TimeUnit.SECONDS)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
String dataUrl = DataConfig.weatherUrl;
|
|
||||||
log.info("dataUrl:" + dataUrl);
|
|
||||||
Request request = new Request.Builder()
|
|
||||||
.url(dataUrl)
|
|
||||||
.get()
|
|
||||||
.build();
|
|
||||||
Response response;
|
|
||||||
String result;
|
|
||||||
try {
|
|
||||||
response = client.newCall(request).execute();
|
|
||||||
log.info("response:" + response);
|
|
||||||
result = response.body().string();
|
|
||||||
log.info("result:" + result);
|
|
||||||
Gson gson = new Gson();
|
|
||||||
WeatherData weatherData = gson.fromJson(result, WeatherData.class);
|
|
||||||
log.info("------weatherData:" + weatherData);
|
|
||||||
WeatherData.WeatherDataChild weatherDataChild = weatherData.getData();
|
|
||||||
log.info("------weatherDataChild:" + weatherDataChild);
|
|
||||||
String city = weatherDataChild.getCity();
|
|
||||||
log.info("city:"+city);
|
|
||||||
String weatherNoon = weatherDataChild.getWeatherNoon();
|
|
||||||
log.info("weatherNoon:"+weatherNoon);
|
|
||||||
String weatherNight = weatherDataChild.getWeatherNight();
|
|
||||||
log.info("weatherNight:"+weatherNight);
|
|
||||||
String minTemperature = weatherDataChild.getMinTemperature();
|
|
||||||
log.info("minTemperature:"+minTemperature);
|
|
||||||
String maxTemperature = weatherDataChild.getMaxTemperature();
|
|
||||||
log.info("maxTemperature:"+maxTemperature);
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new IOException("天气get请求失败", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//自动获取数据测试
|
|
||||||
private static void testGetData() throws ParseException, InterruptedException {
|
|
||||||
AutoWebTask autoWebTask = new AutoWebTask();
|
|
||||||
Thread.sleep(2000);
|
|
||||||
autoWebTask.getCaoLianData(2);
|
|
||||||
Thread.sleep(2000);
|
|
||||||
autoWebTask.getYuJingData(2);
|
|
||||||
Thread.sleep(2000);
|
|
||||||
GetBasicData.getYuJingActionBasicData(2);
|
|
||||||
}
|
|
||||||
|
|
||||||
//日报填写测试
|
|
||||||
private static void testDailrb() {
|
|
||||||
try {
|
|
||||||
//操作日报
|
|
||||||
//日报审核工作
|
|
||||||
AutoWebTask autoWebTask = new AutoWebTask();
|
|
||||||
autoWebTask.dutyAddDailyLogsTask(2,DataConfig.USER_NAME1,DataConfig.PASS1);
|
|
||||||
Thread.sleep(1000);
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//日常操练测试
|
|
||||||
private static void testRCCL() throws InterruptedException {
|
|
||||||
int addExercisePlan = 0;
|
|
||||||
try {
|
|
||||||
AutoWebTask autoWebTask = new AutoWebTask();
|
|
||||||
addExercisePlan = autoWebTask.addExercisePlan("通信测试", "", 0, DataConfig.USER_NAME1, DataConfig.PASS1);
|
|
||||||
Thread.sleep(2000);
|
|
||||||
addExercisePlan = autoWebTask.addExercisePlan("日常操练", "", 0, DataConfig.USER_NAME1, DataConfig.PASS1);
|
|
||||||
Thread.sleep(2000);
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//日志填写测试
|
|
||||||
private static int testLog() {
|
|
||||||
int count = 0;
|
|
||||||
try {
|
|
||||||
//操作日志
|
|
||||||
AutoWebTask autoWebTask = new AutoWebTask();
|
|
||||||
autoWebTask.dutyAddLogsTask(2,DataConfig.USER_NAME1,DataConfig.PASS1);
|
|
||||||
count = 1;
|
|
||||||
Thread.sleep(1000);
|
|
||||||
log.info("count",count);
|
|
||||||
System.out.println(count);
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return count;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 自动化任务
|
|
||||||
*
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
private void autoJob() throws InterruptedException {
|
|
||||||
AutoWebTask autoWebTask = new AutoWebTask();
|
|
||||||
|
|
||||||
int logGatherCount = 0;
|
|
||||||
int dailyGatherCount = 0;
|
|
||||||
int logGatherCount1 = 0;
|
|
||||||
int dailyGatherCount1 = 0;
|
|
||||||
|
|
||||||
int signInzao = 0;
|
|
||||||
int signInzao2 = 0;
|
|
||||||
int signInwan = 0;
|
|
||||||
int signInwan2 = 0;
|
|
||||||
int dailyzao = 0;
|
|
||||||
int dailywan = 0;
|
|
||||||
int jjbzao = 0;
|
|
||||||
int jjbzao2 = 0;
|
|
||||||
int jjbwan = 0;
|
|
||||||
int jjbwan2 = 0;
|
|
||||||
int signOutzao = 0;
|
|
||||||
int signOutzao2 = 0;
|
|
||||||
int signOutwan = 0;
|
|
||||||
int signOutwan2 = 0;
|
|
||||||
int logzao = 0;
|
|
||||||
int logwan = 0;
|
|
||||||
int resetCode = 0;
|
|
||||||
|
|
||||||
int addExercisePlan = 0;
|
|
||||||
|
|
||||||
while (true) {
|
|
||||||
//延迟30s---120s
|
|
||||||
int time = randNum(30, 120);
|
|
||||||
log.info("随机时间为:" + time);
|
|
||||||
try {
|
|
||||||
Thread.sleep(1000 * time);
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("时间", e);
|
|
||||||
}
|
|
||||||
log.info("服务正在运行。。。" + new Date());
|
|
||||||
if(DateTimeUtils.isEffectiveDate("18:10", "18:40")){
|
|
||||||
//进行日报信息系统采集及获取工作
|
|
||||||
try {
|
|
||||||
if (logGatherCount == 0){
|
|
||||||
logGatherCount = autoWebTask.getCaoLianData(1);
|
|
||||||
|
|
||||||
}
|
|
||||||
Thread.sleep(3000);
|
|
||||||
if (dailyGatherCount == 0){
|
|
||||||
dailyGatherCount = autoWebTask.getYuJingData(1);
|
|
||||||
Thread.sleep(2000);
|
|
||||||
GetBasicData.getYuJingActionBasicData(1);
|
|
||||||
}
|
|
||||||
}catch (Exception e) {
|
|
||||||
log.error("信息采集工作", e);
|
|
||||||
}
|
|
||||||
}else if (DateTimeUtils.isEffectiveDate("06:29", "06:59")) {
|
|
||||||
//自动完成当值值班日报(早报)填写上报
|
|
||||||
if (dailyzao == 0) {
|
|
||||||
getTime(900);
|
|
||||||
try {
|
|
||||||
dailyzao = autoWebTask.dutyAddDailyLogsTask(1,DataConfig.USER_NAME1,DataConfig.PASS1);
|
|
||||||
}catch (Exception e){
|
|
||||||
log.error("日报工作", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}else if (DateTimeUtils.isEffectiveDate("07:01", "07:30")) {
|
|
||||||
//完成值班日志填写提交(晚班日志,此次值班日志为总结前一天晚上的情况)
|
|
||||||
if (logzao == 0) {
|
|
||||||
getTime(600);
|
|
||||||
try {
|
|
||||||
logzao = autoWebTask.dutyAddLogsTask(1,DataConfig.USER_NAME1,DataConfig.PASS1);
|
|
||||||
}catch (Exception e){
|
|
||||||
log.error("日志工作", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}else if (DateTimeUtils.isEffectiveDate("07:31", "07:59")) {
|
|
||||||
//自动完成当值值班签到(值班主任、值班人员都要签到)
|
|
||||||
if (signInzao == 0 && signInzao2 == 0){
|
|
||||||
getTime(600);
|
|
||||||
}
|
|
||||||
if (signInzao == 0) {
|
|
||||||
log.info("郭建账号开始打卡任务---------------------");
|
|
||||||
//使用郭建账号签到
|
|
||||||
try {
|
|
||||||
signInzao = autoWebTask.dutySigin(1, 1, DataConfig.USER_NAME1, DataConfig.PASS1);
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("郭建打卡任务", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
Thread.sleep(1000 * 2);
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("打卡任务", e);
|
|
||||||
}
|
|
||||||
if (signInzao2 == 0) {
|
|
||||||
//使用王宗彦账号签到
|
|
||||||
log.info("王宗彦账号开始打卡任务---------------------");
|
|
||||||
try {
|
|
||||||
signInzao2 = autoWebTask.dutySigin(1, 1, DataConfig.USER_NAME2, DataConfig.PASS2);
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("王宗彦打卡任务", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}else if (DateTimeUtils.isEffectiveDate("08:10", "08:20")) {
|
|
||||||
// if (jjbzao == 0 && jjbzao2 == 0){
|
|
||||||
// getTime(420);
|
|
||||||
// }
|
|
||||||
// //自动在系统内完成接班(上一值完成交班后)
|
|
||||||
// if (jjbzao == 0 || jjbzao2 == 0){
|
|
||||||
// if (jjbzao == 0){
|
|
||||||
// jjbzao = autoWebTask.dutyChangeTask1(1,DataConfig.USER_NAME1,DataConfig.PASS1);
|
|
||||||
// }
|
|
||||||
// if (jjbzao2 == 0){
|
|
||||||
// jjbzao2 = autoWebTask.dutyChangeTask2(1,DataConfig.USER_NAME1,DataConfig.PASS1);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
if (jjbzao == 0){
|
|
||||||
getTime(420);
|
|
||||||
}
|
|
||||||
if (jjbzao == 0){
|
|
||||||
jjbzao = autoWebTask.dutyChangeTask(1);
|
|
||||||
}
|
|
||||||
}else if (DateTimeUtils.isEffectiveDate("08:31", "08:40")) {
|
|
||||||
if(signOutzao == 0 && signOutzao2 == 0){
|
|
||||||
getTime(420);
|
|
||||||
}
|
|
||||||
if (signOutzao == 0) {
|
|
||||||
try {
|
|
||||||
signOutzao = autoWebTask.dutySignOutTask(1, DataConfig.USER_NAME1, DataConfig.PASS1);
|
|
||||||
}catch (Exception e){
|
|
||||||
log.error("签退",e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
Thread.sleep(1000 * 3);
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("时间", e);
|
|
||||||
}
|
|
||||||
if (signOutzao2 == 0){
|
|
||||||
try {
|
|
||||||
signOutzao2 = autoWebTask.dutySignOutTask(1, DataConfig.USER_NAME2, DataConfig.PASS2);
|
|
||||||
}catch (Exception e){
|
|
||||||
log.error("签退",e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}else if (DateTimeUtils.isEffectiveDate("08:50", "09:10")){
|
|
||||||
if(addExercisePlan == 0){
|
|
||||||
getTime(1200);
|
|
||||||
}
|
|
||||||
if(addExercisePlan == 0) {
|
|
||||||
addExercisePlan = autoWebTask.addExercisePlan("通信测试", "", 0, DataConfig.USER_NAME1,
|
|
||||||
DataConfig.PASS1);
|
|
||||||
Thread.sleep(1000 * 650);
|
|
||||||
addExercisePlan = autoWebTask.addExercisePlan("日常操练", "", 0, DataConfig.USER_NAME1,
|
|
||||||
DataConfig.PASS1);
|
|
||||||
Thread.sleep(2000);
|
|
||||||
}
|
|
||||||
} else if (DateTimeUtils.isEffectiveDate("12:30", "16:20")) {
|
|
||||||
//进行日志信息系统采集及获取工作
|
|
||||||
try {
|
|
||||||
if (logGatherCount1 == 0){
|
|
||||||
logGatherCount1 = autoWebTask.getCaoLianData(2);
|
|
||||||
}
|
|
||||||
Thread.sleep(3000);
|
|
||||||
if (dailyGatherCount1 == 0){
|
|
||||||
dailyGatherCount1= autoWebTask.getYuJingData(2);
|
|
||||||
Thread.sleep(2000);
|
|
||||||
GetBasicData.getYuJingActionBasicData(2);
|
|
||||||
}
|
|
||||||
}catch (Exception e) {
|
|
||||||
log.error("信息采集工作", e);
|
|
||||||
}
|
|
||||||
}else if (DateTimeUtils.isEffectiveDate("17:01", "17:15")) {
|
|
||||||
//自动完成当值值班日报(晚报)填写上报
|
|
||||||
if (dailywan == 0) {
|
|
||||||
getTime(700);
|
|
||||||
try {
|
|
||||||
dailywan = autoWebTask.dutyAddDailyLogsTask(2,DataConfig.USER_NAME1,DataConfig.PASS1);
|
|
||||||
}catch (Exception e){
|
|
||||||
log.error("日报工作", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}else if (DateTimeUtils.isEffectiveDate("16:30", "16:45")) {
|
|
||||||
//完成值班日志填写提交(白班日志,此次值班日志为总结前一天晚上的情况)
|
|
||||||
if (logwan == 0) {
|
|
||||||
getTime(600);
|
|
||||||
try {
|
|
||||||
logwan = autoWebTask.dutyAddLogsTask(2,DataConfig.USER_NAME1,DataConfig.PASS1);
|
|
||||||
}catch (Exception e){
|
|
||||||
log.error("日志工作", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}else if (DateTimeUtils.isEffectiveDate("16:46", "16:59")) {
|
|
||||||
//自动完成当值值班签到(值班主任、值班人员都要签到)
|
|
||||||
if(signInwan == 0 && signInwan2 == 0){
|
|
||||||
getTime(550);
|
|
||||||
}
|
|
||||||
if (signInwan == 0) {
|
|
||||||
log.info("郭建账号开始打卡任务---------------------");
|
|
||||||
//使用郭建账号签到
|
|
||||||
try {
|
|
||||||
signInwan = autoWebTask.dutySigin(2, 1, DataConfig.USER_NAME1, DataConfig.PASS1);
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("郭建打卡任务", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
Thread.sleep(1000 * 2);
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("打卡任务", e);
|
|
||||||
}
|
|
||||||
if (signInwan2 == 0) {
|
|
||||||
//使用王宗彦账号签到
|
|
||||||
log.info("王宗彦账号开始打卡任务---------------------");
|
|
||||||
try {
|
|
||||||
signInwan2 = autoWebTask.dutySigin(2, 1, DataConfig.USER_NAME2, DataConfig.PASS2);
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("王宗彦打卡任务", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}else if (DateTimeUtils.isEffectiveDate("17:30", "17:45")) {
|
|
||||||
//自动在系统内完成接班(上一值完成交班后)
|
|
||||||
if(jjbwan == 0 && jjbwan2 == 0){
|
|
||||||
getTime(600);
|
|
||||||
}
|
|
||||||
if (jjbwan == 0 || jjbwan2 == 0){
|
|
||||||
if (jjbwan == 0){
|
|
||||||
jjbwan = autoWebTask.dutyChangeTask1(2,DataConfig.USER_NAME1,DataConfig.PASS1);
|
|
||||||
}
|
|
||||||
if (jjbwan2 == 0){
|
|
||||||
jjbwan2 = autoWebTask.dutyChangeTask2(2,DataConfig.USER_NAME1,DataConfig.PASS1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}else if (DateTimeUtils.isEffectiveDate("17:46", "18:00")) {
|
|
||||||
if (signOutwan == 0 && signOutwan2 == 0){
|
|
||||||
getTime(500);
|
|
||||||
}
|
|
||||||
if (signOutwan == 0) {
|
|
||||||
try {
|
|
||||||
signOutwan = autoWebTask.dutySignOutTask(2, DataConfig.USER_NAME1, DataConfig.PASS1);
|
|
||||||
}catch (Exception e){
|
|
||||||
log.error("签退",e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
Thread.sleep(1000 * 3);
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("时间", e);
|
|
||||||
}
|
|
||||||
if (signOutwan2 == 0){
|
|
||||||
try {
|
|
||||||
signOutwan2 = autoWebTask.dutySignOutTask(2, DataConfig.USER_NAME2, DataConfig.PASS2);
|
|
||||||
}catch (Exception e){
|
|
||||||
log.error("签退",e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (DateTimeUtils.isEffectiveDate("20:05", "20:08")) {//重置标识
|
|
||||||
log.info("-----开始重置各标识符-----");
|
|
||||||
logGatherCount = 0;
|
|
||||||
logGatherCount1 = 0;
|
|
||||||
dailyGatherCount = 0;
|
|
||||||
dailyGatherCount1 = 0;
|
|
||||||
signInzao = 0;
|
|
||||||
signInzao2 = 0;
|
|
||||||
signInwan = 0;
|
|
||||||
signInwan2 = 0;
|
|
||||||
dailyzao = 0;
|
|
||||||
dailywan = 0;
|
|
||||||
jjbzao = 0;
|
|
||||||
jjbzao2 = 0;
|
|
||||||
jjbwan = 0;
|
|
||||||
jjbwan2 = 0;
|
|
||||||
signOutzao = 0;
|
|
||||||
signOutzao2 = 0;
|
|
||||||
signOutwan = 0;
|
|
||||||
signOutwan2 = 0;
|
|
||||||
logzao = 0;
|
|
||||||
logwan = 0;
|
|
||||||
resetCode = 0;
|
|
||||||
addExercisePlan = 0;
|
|
||||||
} else {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static List<Integer> getARandomCollectionOfData() {
|
|
||||||
Random random = new Random();
|
|
||||||
int minCount = 3; // 最小数量
|
|
||||||
int maxCount = 5; // 最大数量
|
|
||||||
int count = random.nextInt(maxCount - minCount + 1) + minCount; // 随机生成数量
|
|
||||||
List<Integer> numbers = new ArrayList<>();
|
|
||||||
while (numbers.size() < count) {
|
|
||||||
int randomNumber = random.nextInt(6) + 2; // 随机生成2到7的数字
|
|
||||||
if (!numbers.contains(randomNumber)) {
|
|
||||||
numbers.add(randomNumber);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Collections.shuffle(numbers);
|
|
||||||
return numbers;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void getTime(int code){
|
|
||||||
int time = randNum(0, code);
|
|
||||||
log.info("随机时间为:" + time);
|
|
||||||
try {
|
|
||||||
Thread.sleep(1000 * time);
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("时间", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 随机产生随机数,包含num1和num2
|
|
||||||
*
|
|
||||||
* @param num1
|
|
||||||
* @param num2
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
private int randNum(int num1, int num2) {
|
|
||||||
int result = (int) (num1 + Math.random() * (num2 - num1 + 1));
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public static void start(String text) {
|
|
||||||
log.info("进入播报模式");
|
|
||||||
ActiveXComponent ax = null;
|
|
||||||
try {
|
|
||||||
ax = new ActiveXComponent("Sapi.SpVoice");
|
|
||||||
// 运行时输出语音内容
|
|
||||||
Dispatch spVoice = ax.getObject();
|
|
||||||
// 音量 0-100
|
|
||||||
ax.setProperty("Volume", new Variant(100));
|
|
||||||
// 语音朗读速度 -10 到 +10
|
|
||||||
ax.setProperty("Rate", new Variant(1));
|
|
||||||
// 执行朗读
|
|
||||||
// Dispatch.call(spVoice, "Speak", new Variant(text));
|
|
||||||
// 下面是构建文件流把生成语音文件
|
|
||||||
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("E:\\bns\\audio.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();
|
|
||||||
showVoice();
|
|
||||||
} catch (ComFailException e) {
|
|
||||||
log.error(e.getMessage(), e);
|
|
||||||
log.error("没有可用的音频,请连接外接设备(耳机或音箱播放)");
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
log.error("语音播放错误:" + e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void showVoice() {
|
|
||||||
try {
|
|
||||||
// 获取音频输入流
|
|
||||||
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("E:\\bns\\audio.wav"));
|
|
||||||
// 获取音频格式
|
|
||||||
AudioFormat audioFormat = audioInputStream.getFormat();
|
|
||||||
// 准备数据行格式
|
|
||||||
DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
|
|
||||||
// 打开数据行
|
|
||||||
SourceDataLine dataLine = (SourceDataLine) AudioSystem.getLine(info);
|
|
||||||
dataLine.open(audioFormat);
|
|
||||||
// 开始播放音频
|
|
||||||
dataLine.start();
|
|
||||||
// 缓冲区大小
|
|
||||||
int bufferSize = 4096;
|
|
||||||
byte[] buffer = new byte[bufferSize];
|
|
||||||
int bytesRead = 0;
|
|
||||||
// 从输入流读取数据并写入数据行进行播放
|
|
||||||
while ((bytesRead = audioInputStream.read(buffer)) != -1) {
|
|
||||||
dataLine.write(buffer, 0, bytesRead);
|
|
||||||
}
|
|
||||||
// 等待播放完成
|
|
||||||
dataLine.drain();
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Loading…
Reference in New Issue