语音标记 - Amazon Polly

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

语音标记

以下代码示例显示如何使用基于 Java 的应用程序为输入的文本合成语音标记。此功能使用 SynthesizeSpeech API。

有关此功能的更多信息,请参见语音标记

有关 API 的更多信息,请参阅 SynthesizeSpeech API 参考。

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.SpeechMarkType; 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 SynthesizeSpeechMarksSample { AmazonPolly client = AmazonPollyClientBuilder.defaultClient(); public void synthesizeSpeechMarks() { String outputFileName = "/tmp/speechMarks.json"; SynthesizeSpeechRequest synthesizeSpeechRequest = new SynthesizeSpeechRequest() .withOutputFormat(OutputFormat.Json) .withSpeechMarkTypes(SpeechMarkType.Viseme, SpeechMarkType.Word) .withVoiceId(VoiceId.Joanna) .withText("This is a sample text to be synthesized."); 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); } } }