做业务 哪个网站比较好,公司网站的主页优化,手机网站集成支付宝,php+缺少+wordpress一、业务场景
最近笔者在业务中涉及到语音告警的模块#xff0c;需要将告警内容以文件或流形式返回给前端进行语音播报#xff0c;具体的分析与处理如下
二、业务分析
首先告警内容提示信息这里做的处理是通过专门字段去存储、编辑#xff0c;根据拟定好的代码逻辑判断是…一、业务场景
最近笔者在业务中涉及到语音告警的模块需要将告警内容以文件或流形式返回给前端进行语音播报具体的分析与处理如下
二、业务分析
首先告警内容提示信息这里做的处理是通过专门字段去存储、编辑根据拟定好的代码逻辑判断是否触发语音告警操作后续考虑用哪种方案实现文字转语音功能合适这里笔者选用的是阿里云付费的nls语音服务。
三、解决方案
1、pom文件引入
dependencygroupIdcom.alibaba.nls/groupIdartifactIdnls-sdk-tts/artifactIdversion2.2.1/version
/dependency2、yml文件配置添加
# nls语音配置
ali:nls:##产品相关信息app-key: access-key-id: access-key-secret: ##语音信息声音类型、语速voice: sijiapitch-rate: -68speech-rate: 0
3、实体类构建
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;Data
Component
ConfigurationProperties(prefix ali.nls)
public class NlsProperties {private String appKey;private String accessKeyId;private String accessKeySecret;private String url;//发音人private String voice;//语调范围是-500~500可选默认是0private Integer pitchRate;//语速范围是-500~500默认是0private Integer speechRate;}
4、config类构建
import com.demo.NlsProperties;
import com.demo.NlsTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;Configuration
public class NlsAutoConfiguration {Beanpublic NlsTemplate nlsTemplate(NlsProperties nlsProperties, StringRedisTemplate stringRedisTemplate){return new NlsTemplate(nlsProperties,stringRedisTemplate);}}5、服务类构建
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.nls.client.AccessToken;
import com.alibaba.nls.client.protocol.OutputFormatEnum;
import com.alibaba.nls.client.protocol.SampleRateEnum;
import com.demo.NlsProperties;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.springframework.data.redis.core.StringRedisTemplate;
import java.io.*;
import java.util.Date;Slf4j
RequiredArgsConstructor
public class NlsTemplate {private final NlsProperties nlsProperties;private final StringRedisTemplate stringRedisTemplate;/*** HTTPS POST请求*/public byte[] processPOSTRequest(String text) {/*** 设置HTTPS POST请求* 1.使用HTTPS协议* 2.语音合成服务域名nls-gateway-cn-shanghai.aliyuncs.com* 3.语音合成接口请求路径/stream/v1/tts* 4.设置必须请求参数appkey、token、text、format、sample_rate* 5.设置可选请求参数voice、volume、speech_rate、pitch_rate*/String url https://nls-gateway-cn-shenzhen.aliyuncs.com/stream/v1/tts;JSONObject taskObject new JSONObject();taskObject.putOpt(appkey, nlsProperties.getAppKey());taskObject.putOpt(token, this.getToken());taskObject.putOpt(text, text);taskObject.putOpt(format, OutputFormatEnum.MP3.getName());taskObject.putOpt(voice, nlsProperties.getVoice());taskObject.putOpt(sample_rate, SampleRateEnum.SAMPLE_RATE_16K.value);// speech_rate 语速范围是-500~500可选默认是0。taskObject.putOpt(speech_rate, nlsProperties.getSpeechRate());// pitch_rate 语调范围是-500~500可选默认是0。taskObject.putOpt(pitch_rate, nlsProperties.getPitchRate());String bodyContent JSONUtil.toJsonStr(taskObject);System.out.println(POST Body Content: bodyContent);RequestBody reqBody RequestBody.create(MediaType.parse(application/json), bodyContent);Request request new Request.Builder().url(url).header(Content-Type, application/json).post(reqBody).build();try {OkHttpClient client new OkHttpClient();Response response client.newCall(request).execute();String contentType response.header(Content-Type);byte[] data null;if (audio/mpeg.equals(contentType)) {data response.body().bytes();System.out.println(The POST request succeed!);} else {String errorMessage response.body().string();System.out.println(The POST request failed: errorMessage);}response.close();if (data ! null data.length 0) {// String filePath D:/test.wav; //音频文件保存路径及文件名
// saveFile(data, filePath); //调用函数将音频保存到本地return data;}} catch (Exception e) {e.printStackTrace();}return null;}/*** token暂存redis中*** */SneakyThrowsprivate String getToken() {String key NLS_TOKEN;Boolean hasKey stringRedisTemplate.hasKey(key);if (BooleanUtil.isTrue(hasKey)) {return stringRedisTemplate.opsForValue().get(key);}AccessToken token new AccessToken(nlsProperties.getAccessKeyId(), nlsProperties.getAccessKeySecret());token.apply();String accessToken token.getToken();stringRedisTemplate.opsForValue().set(key, accessToken);stringRedisTemplate.expireAt(key, new Date(token.getExpireTime() * 1000));return accessToken;}/*** 文件转换与生成** */public static void saveFile(byte[] bytes, String filePath) {try {FileOutputStream fos new FileOutputStream(new File(filePath));fos.write(bytes);fos.close();} catch (IOException e) {e.printStackTrace();}}}
具体的配置可以参考官方文档 https://help.aliyun.com/product/30413.html?spma2c4g.750001.0.0.61184fadyUJTlQ 如果服务会频繁使用可以把获取到阿里云token暂存在redis使用。
四、尾言
以上是语音合成业务的大致实现方案后续笔者尝试或有改进的方案也会同步上去感谢阅读和指正。