SynthesizeSpeech - Amazon Polly

Las traducciones son generadas a través de traducción automática. En caso de conflicto entre la traducción y la version original de inglés, prevalecerá la version en inglés.

SynthesizeSpeech

El siguiente ejemplo de código Java muestra cómo utilizar aplicaciones basadas en Java para sintetizar voz con textos más cortos para procesamiento casi en tiempo real.

Para obtener más información, consulte la referencia de la API de SynthesizeSpeech.

package com.amazonaws.polly.samples; import com.amazonaws.services.polly.AmazonPolly; import com.amazonaws.services.polly.AmazonPollyClientBuilder; import com.amazonaws.services.polly.model.OutputFormat; import com.amazonaws.services.polly.model.SynthesizeSpeechRequest; import com.amazonaws.services.polly.model.SynthesizeSpeechResult; import com.amazonaws.services.polly.model.VoiceId; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; public class SynthesizeSpeechSample { AmazonPolly client = AmazonPollyClientBuilder.defaultClient(); public void synthesizeSpeech() { String outputFileName = "/tmp/speech.mp3"; SynthesizeSpeechRequest synthesizeSpeechRequest = new SynthesizeSpeechRequest() .withOutputFormat(OutputFormat.Mp3) .withVoiceId(VoiceId.Joanna) .withText("This is a sample text to be synthesized.") .withEngine("neural"); try (FileOutputStream outputStream = new FileOutputStream(new File(outputFileName))) { SynthesizeSpeechResult synthesizeSpeechResult = client.synthesizeSpeech(synthesizeSpeechRequest); byte[] buffer = new byte[2 * 1024]; int readBytes; try (InputStream in = synthesizeSpeechResult.getAudioStream()){ while ((readBytes = in.read(buffer)) > 0) { outputStream.write(buffer, 0, readBytes); } } } catch (Exception e) { System.err.println("Exception caught: " + e); } } }