2019-02-01 06:27:16 +00:00
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// snippet-start:[polly.java.StartSpeechSynthesisTask.complete]
package com.amazonaws.parrot.service.tests.speech.task ;
import com.amazonaws.parrot.service.tests.AbstractParrotServiceTest ;
import com.amazonaws.services.polly.AmazonPolly ;
import com.amazonaws.services.polly.model.* ;
import org.awaitility.Duration ;
import java.util.concurrent.TimeUnit ;
import static org.awaitility.Awaitility.await ;
public class StartSpeechSynthesisTaskSample {
private static final int SYNTHESIS_TASK_TIMEOUT_SECONDS = 300 ;
private static final AmazonPolly AMAZON_POLLY_CLIENT = AmazonPollyClientBuilder . defaultClient ( ) ;
private static final String PLAIN_TEXT = " This is a sample text to be synthesized. " ;
private static final String OUTPUT_FORMAT_MP3 = OutputFormat . Mp3 . toString ( ) ;
private static final String OUTPUT_BUCKET = " synth-books-buckets " ;
private static final String SNS_TOPIC_ARN = " arn:aws:sns:eu-west-2:561828872312:synthesize-finish-topic " ;
private static final Duration SYNTHESIS_TASK_POLL_INTERVAL = Duration . FIVE_SECONDS ;
private static final Duration SYNTHESIS_TASK_POLL_DELAY = Duration . TEN_SECONDS ;
2024-01-16 10:41:11 -05:00
2019-02-01 06:27:16 +00:00
public static void main ( String . . . args ) {
StartSpeechSynthesisTaskRequest request = new StartSpeechSynthesisTaskRequest ( )
. withOutputFormat ( OUTPUT_FORMAT_MP3 )
. withText ( PLAIN_TEXT )
. withTextType ( TextType . Text )
. withVoiceId ( VoiceId . Amy )
. withOutputS3BucketName ( OUTPUT_BUCKET )
. withSnsTopicArn ( SNS_TOPIC_ARN ) ;
2024-01-16 10:41:11 -05:00
2019-02-01 06:27:16 +00:00
StartSpeechSynthesisTaskResult result = AMAZON_POLLY_CLIENT . startSpeechSynthesisTask ( request ) ;
String taskId = result . getSynthesisTask ( ) . getTaskId ( ) ;
2024-01-16 10:41:11 -05:00
2019-02-01 06:27:16 +00:00
await ( ) . with ( )
. pollInterval ( SYNTHESIS_TASK_POLL_INTERVAL )
. pollDelay ( SYNTHESIS_TASK_POLL_DELAY )
. atMost ( SYNTHESIS_TASK_TIMEOUT_SECONDS , TimeUnit . SECONDS )
. until (
( ) - > getSynthesisTaskStatus ( taskId )
. equals ( TaskStatus . Completed . toString ( ) ) ) ;
}
2024-01-16 10:41:11 -05:00
2019-02-01 06:27:16 +00:00
private static SynthesisTask getSynthesisTask ( String taskId ) {
GetSpeechSynthesisTaskRequest getSpeechSynthesisTaskRequest = new GetSpeechSynthesisTaskRequest ( )
. withTaskId ( taskId ) ;
GetSpeechSynthesisTaskResult result = AMAZON_POLLY_CLIENT
. getSpeechSynthesisTask ( getSpeechSynthesisTaskRequest ) ;
return result . getSynthesisTask ( ) ;
}
2024-01-16 10:41:11 -05:00
2019-02-01 06:27:16 +00:00
private static String getSynthesisTaskStatus ( String taskId ) {
GetSpeechSynthesisTaskRequest getSpeechSynthesisTaskRequest = new GetSpeechSynthesisTaskRequest ( )
. withTaskId ( taskId ) ;
GetSpeechSynthesisTaskResult result = AMAZON_POLLY_CLIENT
. getSpeechSynthesisTask ( getSpeechSynthesisTaskRequest ) ;
return result . getSynthesisTask ( ) . getTaskStatus ( ) ;
}
}
2019-04-12 18:19:26 -07:00
// snippet-end:[polly.java.StartSpeechSynthesisTask.complete]