Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Google TTS Spring Boot Program - Not getting any response

 

 

public class TextToSpeechServiceV2
{
@Value("${audioOutput.fileDirectory}")
private String audioFileDirectory;

@Value("${audioOutput.audioFormat}")
private String audioFileFormat;

@Value("classpath:careful-ensign-380704-cdd798eba5dd.json")
private Resource googleJsonResource;

String audioFileName = "output";

private static final Logger logger = LogManager.getLogger(TextToSpeechServiceV2.class);

public void generateAudioFile(String receivedtext) throws Exception
{
logger.info("V1 Service -> Received Text : " + receivedtext);

// Load the service account key file as a GoogleCredentials object
GoogleCredentials credentials = GoogleCredentials.fromStream(googleJsonResource.getInputStream());

logger.info("Initialised Google Credentials");

// Build the TextToSpeechSettings using the builder pattern and set the service
// account credentials
TextToSpeechSettings settings = TextToSpeechSettings.newBuilder().setCredentialsProvider(() -> credentials)
.build();

logger.info("Build TTS Settings");

// Build the TextToSpeechClient using the GoogleCredentials object
try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create(settings))
{
System.out.println("In Try - Create TTS client");
logger.info("In Try - Create TTS client");
// Set the text input to be synthesized
SynthesisInput input = SynthesisInput.newBuilder().setText(receivedtext).build();

// Build the voice request, select the language code ("en-US") and the ssml
VoiceSelectionParams voice = VoiceSelectionParams.newBuilder().setLanguageCode("en-US")
.setSsmlGender(SsmlVoiceGender.FEMALE).build();
logger.info("Set Voice Params");

// Select the type of audio file you want returned
AudioConfig audioConfig = AudioConfig.newBuilder().setAudioEncoding(AudioEncoding.MP3).build();
logger.info("Audio Configured");

// Perform the text-to-speech request
SynthesizeSpeechResponse response = textToSpeechClient.synthesizeSpeech(input, voice, audioConfig);
logger.info("Audio Synthesised");

// Get the audio contents from the response
ByteString audioContents = response.getAudioContent();

// Write the response to the output file
try (OutputStream out = new FileOutputStream(audioFileDirectory + audioFileName + audioFileFormat))
{
out.write(audioContents.toByteArray());
logger.info("File Opened and Written");
} catch (Exception e)
{
logger.error("Exception in Opening File : " + e.getMessage());
logger.error(e.getMessage(), e);
throw e;
}
}catch (Exception e) {
logger.error("Exception in TTS Client Create : " + e.getMessage());
logger.error(e.getMessage(), e);
throw e;
}
}
}

 

 

 

 

The program stops at the line

 

SynthesizeSpeechResponse response = textToSpeechClient.synthesizeSpeech(input, voice, audioConfig);

 

Does not get any response from there. When I am testing with Postman, I get a Gateway Timeout Error and if I check for logs, anything after this line is not coming. No exceptions. Nothing. 

Like the program just stops.

 

Please help. Thanks in advance

0 7 863
7 REPLIES 7