Commit d94478a6b643c35aa587e6fc9a89c832e32b4cc7

Authored by 王通
1 parent ab3d47f3

1.

src/main/java/com/bsth/util/IFlyUtils.java
... ... @@ -37,6 +37,10 @@ public class IFlyUtils {
37 37 * @param text
38 38 */
39 39 public static void textToSpeechCn(String text, String outputPath) throws Exception {
  40 + File file = new File(outputPath);
  41 + if (!file.getParentFile().exists()) {
  42 + file.getParentFile().mkdirs();
  43 + }
40 44 String wsUrl = getAuthUrl("https://tts-api.xfyun.cn/v2/tts", apiKey, apiSecret).replace("https://", "wss://");
41 45 SpeechRequest request = new SpeechRequest();
42 46 request.getCommon().put("app_id", appId);
... ... @@ -45,7 +49,7 @@ public class IFlyUtils {
45 49 request.getBusiness().put("vcn", "x4_lingxiaoshan_profnews");
46 50 request.getData().put("text", Base64.getEncoder().encodeToString(text.replace(",", "[p1000]").getBytes("GBK")));
47 51 request.getData().put("status", 2);
48   - websocketWork(wsUrl, request, new FileOutputStream(outputPath));
  52 + websocketWork(wsUrl, request, new FileOutputStream(file));
49 53 while (!request.isCompleted()) {
50 54 Thread.sleep(500);
51 55 }
... ... @@ -57,6 +61,10 @@ public class IFlyUtils {
57 61 * @param text
58 62 */
59 63 public static void textToSpeechSh(String text, String outputPath) throws Exception {
  64 + File file = new File(outputPath);
  65 + if (!file.getParentFile().exists()) {
  66 + file.getParentFile().mkdirs();
  67 + }
60 68 String wsUrl = getAuthUrl("https://tts-api.xfyun.cn/v2/tts", apiKey, apiSecret).replace("https://", "wss://");
61 69 SpeechRequest request = new SpeechRequest();
62 70 request.getCommon().put("app_id", appId);
... ... @@ -65,7 +73,7 @@ public class IFlyUtils {
65 73 request.getBusiness().put("vcn", "x3_ziling");
66 74 request.getData().put("text", Base64.getEncoder().encodeToString(text.replace(",", "[p1000]").getBytes("GBK")));
67 75 request.getData().put("status", 2);
68   - websocketWork(wsUrl, request, new FileOutputStream(outputPath));
  76 + websocketWork(wsUrl, request, new FileOutputStream(file));
69 77 while (!request.isCompleted()) {
70 78 Thread.sleep(500);
71 79 }
... ... @@ -77,6 +85,10 @@ public class IFlyUtils {
77 85 * @param text
78 86 */
79 87 public static void textToSpeechEn(String text, String outputPath) throws Exception {
  88 + File file = new File(outputPath);
  89 + if (!file.getParentFile().exists()) {
  90 + file.getParentFile().mkdirs();
  91 + }
80 92 String wsUrl = getAuthUrl("https://tts-api.xfyun.cn/v2/tts", apiKey, apiSecret).replace("https://", "wss://");
81 93 SpeechRequest request = new SpeechRequest();
82 94 request.getCommon().put("app_id", appId);
... ... @@ -85,7 +97,7 @@ public class IFlyUtils {
85 97 request.getBusiness().put("vcn", "x4_enus_luna_assist");
86 98 request.getData().put("text", Base64.getEncoder().encodeToString(text.replace(",", "[p1000]").getBytes(StandardCharsets.UTF_8)));
87 99 request.getData().put("status", 2);
88   - websocketWork(wsUrl, request, new FileOutputStream(outputPath));
  100 + websocketWork(wsUrl, request, new FileOutputStream(file));
89 101 while (!request.isCompleted()) {
90 102 Thread.sleep(500);
91 103 }
... ...