Browse Source

生成语音文件不再使用动态库,使用讯飞api

lighter 3 năm trước cách đây
mục cha
commit
6aa54ba1af

+ 5 - 5
pom.xml

@@ -159,11 +159,6 @@
             <artifactId>wechatpay-apache-httpclient</artifactId>
             <version>0.2.1</version>
         </dependency>
-        <dependency>
-            <groupId>com.hynnet</groupId>
-            <artifactId>jacob</artifactId>
-            <version>1.18</version>
-        </dependency>
         <dependency>
             <groupId>org.apache.poi</groupId>
             <artifactId>poi</artifactId>
@@ -197,6 +192,11 @@
             <version>RELEASE</version>
             <scope>compile</scope>
         </dependency>
+        <dependency>
+            <groupId>cn.xfyun</groupId>
+            <artifactId>websdk-java-speech</artifactId>
+            <version>2.0.1</version>
+        </dependency>
     </dependencies>
     <build>
         <plugins>

+ 6 - 0
src/main/java/thyyxxk/webserver/utils/Test.java

@@ -0,0 +1,6 @@
+package thyyxxk.webserver.utils;
+
+public class Test {
+
+
+}

+ 61 - 59
src/main/java/thyyxxk/webserver/utils/VoiceUtil.java

@@ -1,19 +1,25 @@
 package thyyxxk.webserver.utils;
 
-import com.jacob.activeX.ActiveXComponent;
-import com.jacob.com.Dispatch;
-import com.jacob.com.Variant;
+import cn.xfyun.api.TtsClient;
+import cn.xfyun.model.response.TtsResponse;
+import cn.xfyun.service.tts.AbstractTtsWebSocketListener;
 import lombok.extern.slf4j.Slf4j;
+import okhttp3.Response;
+import okhttp3.WebSocket;
 import thyyxxk.webserver.config.exception.ExceptionEnum;
 import thyyxxk.webserver.entity.ResultVo;
 
 import java.io.File;
+import java.util.concurrent.TimeUnit;
 
 /**
  * @author dj
  */
 @Slf4j
 public class VoiceUtil {
+    private static final String appId = "21cb4067";
+    private static final String apiKey = "c49c636d0a771bc694d75d3433cd84e3";
+    private static final String apiSecret = "MjAzNTk3MDRiODc2NmU2ZjMxMTBhYzZl";
 
     private static void deleteOldFile(String serialNo) {
         String path = "D:\\apache-tomcat-9.0.30\\webapps\\voices\\" + serialNo + ".mp3";
@@ -27,67 +33,63 @@ public class VoiceUtil {
 
     public static ResultVo<String> textToSpeech(String data, String id) {
         deleteOldFile(id);
-        ActiveXComponent ax = null;
-        Dispatch spVoice = null;
-        Dispatch spAudioFormat = null;
-        Dispatch spFileStream = null;
         String voiceUrl = "http://webhis.thyy.cn:8888/voices/" + id + ".mp3";
         String filePath = "D:\\apache-tomcat-9.0.30\\webapps\\voices\\" + id + ".mp3";
-        try {
-            ax = new ActiveXComponent("Sapi.SpVoice");
-            // 运行时输出语音内容
-            spVoice = ax.getObject();
-            // 音量 0-100
-            ax.setProperty("Volume", new Variant(100));
-            // 语音朗读速度 -10 到 +10
-            ax.setProperty("Rate", new Variant(-1));
-            // 下面是构建文件流把生成语音文件
-            ax = new ActiveXComponent("Sapi.SpFileStream");
-            spFileStream = ax.getObject();
+        final Boolean[] progress = { false, null };
+        final String[] message = {""};
+        try  {
+            TtsClient ttsClient = new TtsClient.Builder()
+                    .signature(appId, apiKey, apiSecret)
+                    .vcn("aisjinger")
+                    .volume(100)
+                    .build();
+            File file = new File(filePath);
+            ttsClient.send(data, new AbstractTtsWebSocketListener(file) {
+                @Override
+                public void onSuccess(byte[] bytes) {
+                    progress[0] = true;
+                }
+                @Override
+                public void onFail(WebSocket webSocket, Throwable throwable, Response response) {
+                    log.error("生成语音文件失败:{}", throwable.getMessage());
+                    message[0] = throwable.getMessage();
+                }
 
-            ax = new ActiveXComponent("Sapi.SpAudioFormat");
-            spAudioFormat = ax.getObject();
+                @Override
+                public void onBusinessFail(WebSocket webSocket, TtsResponse ttsResponse) {
+                    log.error("生成语音文件失败:{}", ttsResponse.toString());
+                    message[0] = ttsResponse.toString();
+                }
 
-            // 设置音频流格式
-            Dispatch.put(spAudioFormat, "Type", new Variant(22));
-            // 设置文件输出流格式
-            Dispatch.putRef(spFileStream, "Format", spAudioFormat);
-            // 调用输出 文件流打开方法,创建一个.wav文件
-            Dispatch.call(spFileStream, "Open",
-                    new Variant(filePath),
-                    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(-2));
-            // 开始朗读
-            Dispatch.call(spVoice, "Speak", new Variant(data));
-            // 关闭输出文件
-            Dispatch.call(spFileStream, "Close");
-            Dispatch.putRef(spVoice, "AudioOutputStream", null);
-            return ResultVoUtil.success(voiceUrl);
-        } catch (Exception e) {
-            File file = new File(filePath);
-            if (file.exists()) {
-                return ResultVoUtil.success(voiceUrl);
-            }
-            String message = "转换语音出错,错误原因:" + e.getMessage();
-            return ResultVoUtil.fail(ExceptionEnum.INTERNAL_SERVER_ERROR, message);
-        } finally {
-            if (null != spAudioFormat) {
-                spAudioFormat.safeRelease();
-            }
-            if (null != spFileStream) {
-                spFileStream.safeRelease();
-            }
-            if (null != spVoice) {
-                spVoice.safeRelease();
-            }
-            if (null != ax) {
-                ax.safeRelease();
+                @Override
+                public void onClosing(WebSocket webSocket, int code, String reason) {
+                    super.onClosing(webSocket, code, reason);
+                }
+
+                @Override
+                public void onClosed(WebSocket webSocket, int code, String reason) {
+                    super.onClosed(webSocket, code, reason);
+                    progress[1] = true;
+                }
+            });
+        }catch (Exception e){
+            log.error("生成语音文件失败", e);
+            log.error("错误码查询链接:https://www.xfyun.cn/document/error-code");
+            message[0] = e.getMessage();
+            progress[1] = true;
+        }
+
+        while (null == progress[1]) {
+            try {
+                TimeUnit.MILLISECONDS.sleep(1000);
+            } catch (InterruptedException e) {
+                e.printStackTrace();
             }
         }
+
+        if (progress[0]) {
+            return ResultVoUtil.success(voiceUrl);
+        }
+        return ResultVoUtil.fail(ExceptionEnum.INTERNAL_SERVER_ERROR, message[0]);
     }
 }