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