javagoogle-text-to-speech

Java Google Text to Speech: Play Reponse, Do not Convert to mp3


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\"");
    }

}

}


Solution

  • 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();