Pu Zhibing
5 小时以前 821fc2f632f8b974a2c0fc37630e13c5fbe6086c
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){