本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
Java 範例
此範例示範如何使用 Amazon Polly 從 Java 型應用程式串流語音。此範例使用 AWS SDK for Java
顯示的程式碼包括主要工作,但僅進行最基本的錯誤檢查。如果 Amazon Polly 遇到錯誤,應用程式會終止。
若要執行此範例應用程式,您需要以下項目:
-
Java 8 Java 開發套件 (JDK)
若要測試應用程式
-
確定已為 設定 JAVA_HOME 環境變數JDK。
例如,如果您在 JDK 的 Windows 上安裝 1.8.0_121
C:\Program Files\Java\jdk1.8.0_121
,請在命令提示中輸入以下內容:set JAVA_HOME=""C:\Program Files\Java\jdk1.8.0_121""
如果您在 JDK 的 Linux 中安裝
/usr/lib/jvm/java8-openjdk-amd64
1.8.0_121,請在命令提示中輸入以下內容:export JAVA_HOME=/usr/lib/jvm/java8-openjdk-amd64
-
設定 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
-
建立名為
polly-java-demo
的新目錄。 -
在
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>
-
在
src/main/java/com/amazonaws/demos
建立名為polly
的新目錄。 -
在
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).withEngine("neural"); 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(); } }
-
回到
polly-java-demo
目錄清理、編譯和執行示範:mvn clean compile exec:java