0

I am using the google text-to-speech API and I am trying to figure out how I'd be able to play the google response immediately rather than converting it to a mp3 file

 public static void TTS(String word) throws IOException {
    authExplicit();
    try (


    // Set the text input to be synthesized
    SynthesisInput input = SynthesisInput.newBuilder().setText(word).build();

    // Build the voice request, select the language code ("en-US") and the ssml voice gender
    // ("neutral")
    VoiceSelectionParams voice =
            VoiceSelectionParams.newBuilder()
                    .setLanguageCode("en-US")
                    .setSsmlGender(SsmlVoiceGender.NEUTRAL)
                    .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();


    // HERE, I DO NOT WANT TO CONVERT TO MP3. I just want the audio played out.....
    try (OutputStream out = new FileOutputStream("output.mp3")) {
        out.write(audioContents.toByteArray());

        System.out.println("Audio content written to file \"output.mp3\"");
    }

}

}

Saheed
  • 301
  • 1
  • 2
  • 11
  • Did you try playing the audio file as mentioned [here](https://stackoverflow.com/questions/6045384/playing-mp3-and-wav-in-java)? – Sakshi Gatyan Apr 12 '22 at 05:03
  • Similar Stack thread: https://stackoverflow.com/questions/58310377/is-it-possible-to-synthesize-text-to-speech-without-writing-a-new-file – Sakshi Gatyan Apr 12 '22 at 05:06

1 Answers1

1

i fixed this, i added a jplayer to my dependencies then i replaced then i replaced the mp3 part with this:

BufferedInputStream inputStream = new BufferedInputStream(new ByteArrayInputStream(audioContents.toByteArray()));
        Player player = new Player(inputStream); //player from jplayer
        player.play();
Saheed
  • 301
  • 1
  • 2
  • 11