| | |
| | | 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; |
| | | |
| | |
| | | * @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(); |
| | | |
| | |
| | | ByteString audioContents = response.getAudioContent(); |
| | | |
| | | // Write the response to the output file. |
| | | try (OutputStream out = new FileOutputStream("/usr/local/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 "http://182.160.16.251:81/files/audio/" + fileName; |
| | | return "https://igo.i-go.group/files/audio/" + fileName; |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | } |