1. 内网环境 windows系统 选择jacob技术实现 免费的
从官网下载最新1.20jar包和dll文件
将jar包放到maven仓库中
dll文件放到jdk的bin目录下
项目代码:
package com.example.ybxm.controller;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Objects;
import java.util.UUID;
import java.io.IOException;
import static org.aspectj.weaver.tools.cache.SimpleCacheFactory.path;
/**
* 离线 本地 语音朗读
*/
public class VoiceReading {
//输入文本内容,生成文件地址 text为输入的文本信息
public static void audioFile(String text){
try {
//jacob.dll没成功安装,执行这一步会出错
//构建音频格式 调用注册表应用
Dispatch spAudioFormat = new ActiveXComponent("Sapi.SpAudioFormat").getObject();
//音频文件输出流
Dispatch spFileStream = new ActiveXComponent("Sapi.SpFileStream").getObject();
//构建音频对象
Dispatch spVoice = new ActiveXComponent("Sapi.SpVoice").getObject();
// Dispatch spObjectToken = Dispatch.call(spVoice, "GetVoices").toDispatch();
// Dispatch voice = Dispatch.call(spObjectToken, "Item", new Variant(0)).toDispatch();
//设置spAudioFormat音频流格式类型22
Dispatch.put(spAudioFormat, "Type", new Variant(0));
//设置spFileStream文件输出流的音频格式
Dispatch.putRef(spFileStream, "Format", spAudioFormat);
// Dispatch.put(spVoice, "Voice", voice);
//设置spFileStream文件输出流参数地址等
Dispatch.call(spFileStream, "Open", new Variant("D:\\java\\景天\\自助拿号打印服务\\text\\48641486.wav"), new Variant(3), new Variant(true));
//设置spVoice声音对象的音频输出流为输出文件对象
Dispatch.putRef(spVoice, "AudioOutputStream", spFileStream);
//设置spVoice声音对象的音量大小100
Dispatch.put(spVoice, "Volume", new Variant(100));
//设置spVoice声音对象的速度 0为正常速度,范围【..-2 -1 0 1 2..】
Dispatch.put(spVoice, "Rate", new Variant(0));
//设置spVoice声音对象中的文本内容
Dispatch.call(spVoice, "Speak", new Variant(text));
//关闭spFileStream输出文件
Dispatch.call(spFileStream, "Close");
//释放资源
spVoice.safeRelease();
spAudioFormat.safeRelease();
spFileStream.safeRelease();
}catch (Exception e){
System.out.println(e.getMessage());
}
}
}