From b35b859957e3f8aac2f7d70b7a13f5eb16056840 Mon Sep 17 00:00:00 2001 From: Pu Zhibing <393733352@qq.com> Date: 星期五, 25 四月 2025 18:25:48 +0800 Subject: [PATCH] 修改bug --- ManagementIGOTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/TextToSpeechUtil.java | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 60 insertions(+), 0 deletions(-) diff --git a/ManagementIGOTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/TextToSpeechUtil.java b/ManagementIGOTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/TextToSpeechUtil.java new file mode 100644 index 0000000..97857c2 --- /dev/null +++ b/ManagementIGOTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/TextToSpeechUtil.java @@ -0,0 +1,60 @@ +package com.stylefeng.guns.modular.system.util; + +import com.google.cloud.texttospeech.v1.*; +import com.google.protobuf.ByteString; + +import java.io.FileOutputStream; +import java.io.OutputStream; + +/** + * Google语音合成工具 + * @author zhibing.pu + * @Date 2024/7/11 9:09 + */ +public class TextToSpeechUtil { + + + /** + * 合成音频文件 + * @param languageCode 语言编号 + * @param text 合成文本 + * @param fileName 音频文件名称 + * @throws Exception + */ + public static String create(String languageCode, String text, String fileName) throws Exception { + // Instantiates a client + try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) { + // Set the text input to be synthesized + SynthesisInput input = SynthesisInput.newBuilder().setText(text).build(); + + // Build the voice request, select the language code ("en-US") and the ssml voice gender + // ("neutral") + VoiceSelectionParams voice = + VoiceSelectionParams.newBuilder() + .setLanguageCode(languageCode) + .setSsmlGender(SsmlVoiceGender.MALE) + .build(); + + // Select the type of audio file you want returned + AudioConfig audioConfig = + AudioConfig.newBuilder().setAudioEncoding(AudioEncoding.MP3).build(); + + // Perform the text-to-speech request on the text input with the selected voice parameters and + // audio file type + SynthesizeSpeechResponse response = + textToSpeechClient.synthesizeSpeech(input, voice, audioConfig); + + // Get the audio contents from the response + ByteString audioContents = response.getAudioContent(); + + // Write the response to the output file. + try (OutputStream out = new FileOutputStream("/home/igotechgh/nginx/html/files/audio/" + fileName)) { + out.write(audioContents.toByteArray()); + return "https://igo.i-go.group/files/audio/" + fileName; + }catch (Exception e){ + e.printStackTrace(); + } + return null; + } + } +} -- Gitblit v1.7.1