Amazon Polly
开发人员指南
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 AWS 服务入门

Java 示例

本示例显示了如何使用 Amazon Polly 从基于 Java 的应用程序流式传输语音。该示例使用 适用于 Java 的 AWS 软件开发工具包、利用从列表中选择的语音读取指定文本。

显示的代码涵盖主要任务,但只能进行微小错误检查。如果 Amazon Polly 遇到错误,应用程序将终止。

要运行此示例应用程序,您需要以下条件:

测试应用程序

  1. 确保针对 JDK 设置了 JAVA_HOME 环境变量。

    例如,如果您在 Windows 上安装了 JDK 1.8.0_121 (路径为 C:\Program Files\Java\jdk1.8.0_121),则您应在命令提示符下键入以下内容:

    set JAVA_HOME=""C:\Program Files\Java\jdk1.8.0_121""

    如果您在 Linux 中安装了 JDK 1.8.0_121 (路径为/usr/lib/jvm/java8-openjdk-amd64),您可以在命令提示符下键入以下内容:

    export JAVA_HOME=/usr/lib/jvm/java8-openjdk-amd64
  2. 设置 Maven 环境变量以便从命令行中运行 Maven。

    例如,如果您在 Windows 上安装了 Maven 3.3.9 (路径为 C:\Program Files\apache-maven-3.3.9),则您可以键入以下内容:

    set M2_HOME=""C:\Program Files\apache-maven-3.3.9"" set M2=%M2_HOME%\bin set PATH=%M2%;%PATH%

    如果您在 Linux 上安装了 Maven 3.3.9 (路径为 /home/ec2-user/opt/apache-maven-3.3.9),则您可以键入以下内容:

    export M2_HOME=/home/ec2-user/opt/apache-maven-3.3.9 export M2=$M2_HOME/bin export PATH=$M2:$PATH
  3. 创建名为 polly-java-demo 的新目录。

  4. polly-java-demo 目录中,创建一个名为 pom.xml 的新文件,并粘贴以下代码:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.amazonaws.polly</groupId> <artifactId>java-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-polly --> <dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sdk-polly</artifactId> <version>1.11.77</version> </dependency> <!-- https://mvnrepository.com/artifact/com.googlecode.soundlibs/jlayer --> <dependency> <groupId>com.googlecode.soundlibs</groupId> <artifactId>jlayer</artifactId> <version>1.0.1-1</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.2.1</version> <executions> <execution> <goals> <goal>java</goal> </goals> </execution> </executions> <configuration> <mainClass>com.amazonaws.demos.polly.PollyDemo</mainClass> </configuration> </plugin> </plugins> </build> </project>
  5. src/main/java/com/amazonaws/demos 创建名为 polly 的新目录。

  6. polly 目录中,创建一个名为 PollyDemo.java 的新 Java 源文件,并粘贴以下代码:

    package com.amazonaws.demos.polly; import java.io.IOException; import java.io.InputStream; import com.amazonaws.ClientConfiguration; import com.amazonaws.auth.DefaultAWSCredentialsProviderChain; import com.amazonaws.regions.Region; import com.amazonaws.regions.Regions; import com.amazonaws.services.polly.AmazonPollyClient; import com.amazonaws.services.polly.model.DescribeVoicesRequest; import com.amazonaws.services.polly.model.DescribeVoicesResult; 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.Voice; import javazoom.jl.player.advanced.AdvancedPlayer; import javazoom.jl.player.advanced.PlaybackEvent; import javazoom.jl.player.advanced.PlaybackListener; public class PollyDemo { private final AmazonPollyClient polly; private final Voice voice; private static final String SAMPLE = "Congratulations. You have successfully built this working demo of Amazon Polly in Java. Have fun building voice enabled apps with Amazon Polly (that's me!), and always look at the AWS website for tips and tricks on using Amazon Polly and other great services from AWS"; public PollyDemo(Region region) { // create an Amazon Polly client in a specific region polly = new AmazonPollyClient(new DefaultAWSCredentialsProviderChain(), new ClientConfiguration()); polly.setRegion(region); // Create describe voices request. DescribeVoicesRequest describeVoicesRequest = new DescribeVoicesRequest(); // Synchronously ask Amazon Polly to describe available TTS voices. DescribeVoicesResult describeVoicesResult = polly.describeVoices(describeVoicesRequest); voice = describeVoicesResult.getVoices().get(0); } public InputStream synthesize(String text, OutputFormat format) throws IOException { SynthesizeSpeechRequest synthReq = new SynthesizeSpeechRequest().withText(text).withVoiceId(voice.getId()) .withOutputFormat(format); SynthesizeSpeechResult synthRes = polly.synthesizeSpeech(synthReq); return synthRes.getAudioStream(); } public static void main(String args[]) throws Exception { //create the test class PollyDemo helloWorld = new PollyDemo(Region.getRegion(Regions.US_EAST_1)); //get the audio stream InputStream speechStream = helloWorld.synthesize(SAMPLE, OutputFormat.Mp3); //create an MP3 player AdvancedPlayer player = new AdvancedPlayer(speechStream, javazoom.jl.player.FactoryRegistry.systemRegistry().createAudioDevice()); player.setPlayBackListener(new PlaybackListener() { @Override public void playbackStarted(PlaybackEvent evt) { System.out.println("Playback started"); System.out.println(SAMPLE); } @Override public void playbackFinished(PlaybackEvent evt) { System.out.println("Playback finished"); } }); // play it! player.play(); } }
  7. 返回到 polly-java-demo 目录,以清除、编译和执行演示:

    mvn clean compile exec:java