Files
nas-service/src/main/java/com/yutou/nas/utils/AudioTools.java
Yutousama 301dc63624 新增门监控控制
ups电池获取单位改成int
2022-03-17 09:22:57 +08:00

117 lines
4.0 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package com.yutou.nas.utils;
import com.iflytek.cloud.speech.*;
import com.yutou.nas.interfaces.ObjectInterface;
import java.io.File;
public class AudioTools {
private static boolean init = false;
synchronized static void init() {
if (init) {
return;
}
SpeechUtility.createUtility(SpeechConstant.APPID + "=601f7f7d");
SpeechUtility.getUtility().setParameter(SpeechConstant.VOLUME,"100");
SpeechUtility.getUtility().setParameter(SpeechConstant.LIB_NAME_64,"/media/yutou/disk_lvm/public/servier/tools/");
SpeechUtility.getUtility().setParameter(SpeechConstant.LIB_NAME_32,"/media/yutou/disk_lvm/public/servier/tools/");
init = true;
com.yutou.nas.utils.Log.i("讯飞语音已初始化");
}
public static void playText(String text) {
SpeechSynthesizer mss = SpeechSynthesizer.createSynthesizer();
mss.startSpeaking(text, new SynthesizerListener() {
@Override
public void onBufferProgress(int progress, int beginPos, int endPos,
String info) {
if (progress == 100) {
mss.destroy();
}
}
@Override
public void onSpeakBegin() {
}
@Override
public void onSpeakProgress(int i, int i1, int i2) {
}
@Override
public void onSpeakPaused() {
}
@Override
public void onSpeakResumed() {
}
@Override
public void onCompleted(SpeechError speechError) {
com.yutou.nas.utils.Log.i(speechError.getErrorDesc() + " code " + speechError.getErrorCode());
}
@Override
public void onEvent(int i, int i1, int i2, int i3, Object o, Object o1) {
}
});
}
public static void getAudio(String text, ObjectInterface objectInterface){
SpeechSynthesizer mss = SpeechSynthesizer.createSynthesizer();
mss.setParameter(SpeechConstant.VOICE_NAME, "xiaoyu");
// 设置语速范围0~100
mss.setParameter(SpeechConstant.SPEED, "50");
// 设置语调范围0~100
//mss.setParameter(SpeechConstant.PITCH, "50");
// 设置音量范围0~100
mss.setParameter(SpeechConstant.VOLUME, "100");
String fileName="html" + File.separator + "audio" + File.separator + System.currentTimeMillis() + ".wav";
mss.synthesizeToUri(
text,
fileName,
new SynthesizeToUriListener() {
@Override
public void onBufferProgress(int progress) {
System.out.println("progress = " + progress);
if (progress == 100) {
objectInterface.out(fileName);
mss.destroy();
}
}
@Override
public void onSynthesizeCompleted(String s, SpeechError speechError) {
System.out.println("s = " + s);
System.out.println("speechError = " + speechError.getErrorDesc());
}
@Override
public void onEvent(int i, int i1, int i2, int i3, Object o, Object o1) {
System.out.println("i = " + i + ", i1 = " + i1 + ", i2 = " + i2 + ", i3 = " + i3 + ", o = " + o + ", o1 = " + o1);
}
}
);
}
public static void main(String[] args) {
init();
getAudio("警告!你已经进入监控范围,请立即离开!", new ObjectInterface() {
@Override
public void out(String data) {
super.out(data);
System.out.println(data);
File file=new File(data);
System.out.println(file.getAbsolutePath());
}
});
}
}