From 821fc2f632f8b974a2c0fc37630e13c5fbe6086c Mon Sep 17 00:00:00 2001 From: Pu Zhibing <393733352@qq.com> Date: 星期五, 26 九月 2025 17:26:16 +0800 Subject: [PATCH] 修改bug --- ManagementIGOTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/TextToSpeechUtil.java | 19 ++++++++++++++++--- 1 files changed, 16 insertions(+), 3 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 index 97857c2..328c227 100644 --- 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 @@ -1,8 +1,12 @@ package com.stylefeng.guns.modular.system.util; +import com.google.api.gax.core.FixedCredentialsProvider; +import com.google.auth.oauth2.GoogleCredentials; import com.google.cloud.texttospeech.v1.*; import com.google.protobuf.ByteString; +import java.io.File; +import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.OutputStream; @@ -22,8 +26,13 @@ * @throws Exception */ public static String create(String languageCode, String text, String fileName) throws Exception { - // Instantiates a client - try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) { + GoogleCredentials googleCredentials = GoogleCredentials + .fromStream(new FileInputStream("/home/igotechgh/i-go-gcp-f83e2d02152a.json")) + .createScoped("https://www.googleapis.com/auth/cloud-platform"); + + FixedCredentialsProvider provider = FixedCredentialsProvider.create(googleCredentials); + TextToSpeechSettings textToSpeechSettings = TextToSpeechSettings.newBuilder().setCredentialsProvider(provider).build(); + try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create(textToSpeechSettings)) { // Set the text input to be synthesized SynthesisInput input = SynthesisInput.newBuilder().setText(text).build(); @@ -48,7 +57,11 @@ ByteString audioContents = response.getAudioContent(); // Write the response to the output file. - try (OutputStream out = new FileOutputStream("/home/igotechgh/nginx/html/files/audio/" + fileName)) { + File file = new File("/data/nginx/html/files/audio/" + fileName); + if (!file.exists()) { + file.createNewFile(); + } + try (OutputStream out = new FileOutputStream("/data/nginx/html/files/audio/" + fileName)) { out.write(audioContents.toByteArray()); return "https://igo.i-go.group/files/audio/" + fileName; }catch (Exception e){ -- Gitblit v1.7.1