aws java_AWS Polly Java example

I am trying to execute a simple Java example of AWS Polly service,

I am using the code provided by AWS on their documentation,

I created a simple Maven Project using the following -

1. group id - com.amazonaws.polly

2. artifact id - java-demo

3. version - 0.0.1-SNAPSHOT

Following is my project structure -

bnzK4.png

Following is my pom.xml -

4.0.0

com.amazonaws.polly

java-demo

0.0.1-SNAPSHOT

com.amazonaws

aws-java-sdk-polly

1.11.77

com.googlecode.soundlibs

jlayer

1.0.1-1

org.codehaus.mojo

exec-maven-plugin

1.2.1

java

com.amazonaws.demos.polly.PollyDemo

Following is my java class -

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) {

//Didn't work

//AWSCredentials credentials = new BasicAWSCredentials("someAccessKey","someSecretKey");

//polly = new AmazonPollyClient(credentials);

//Didn't work

// 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();

}

}

I am running the code locally, therefore I have my AWS IAM credentials configured in my system,

My IAM user also has access to AWS Polly service.

I am getting the following error when I run the code -

Exception in thread "main" com.amazonaws.services.polly.model.AmazonPollyException: The security token included in the request is invalid. (Service: AmazonPolly; Status Code: 403; Error Code: UnrecognizedClientException; Request ID: 4d4b01fb-8015-11e8-8e18-4548f95fba92)

at com.amazonaws.http.AmazonHttpClient$RequestExecutor.handleErrorResponse(AmazonHttpClient.java:1586)

at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeOneRequest(AmazonHttpClient.java:1254)

at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeHelper(AmazonHttpClient.java:1035)

at com.amazonaws.http.AmazonHttpClient$RequestExecutor.doExecute(AmazonHttpClient.java:747)

at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeWithTimer(AmazonHttpClient.java:721)

at com.amazonaws.http.AmazonHttpClient$RequestExecutor.execute(AmazonHttpClient.java:704)

at com.amazonaws.http.AmazonHttpClient$RequestExecutor.access$500(AmazonHttpClient.java:672)

at com.amazonaws.http.AmazonHttpClient$RequestExecutionBuilderImpl.execute(AmazonHttpClient.java:654)

at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:518)

at com.amazonaws.services.polly.AmazonPollyClient.doInvoke(AmazonPollyClient.java:668)

at com.amazonaws.services.polly.AmazonPollyClient.invoke(AmazonPollyClient.java:644)

at com.amazonaws.services.polly.AmazonPollyClient.describeVoices(AmazonPollyClient.java:383)

at com.amazonaws.demos.polly.PollyDemo.(PollyDemo.java:39)

at com.amazonaws.demos.polly.PollyDemo.main(PollyDemo.java:54)

I am referring the following AWS Doc for the Polly java example- https://docs.aws.amazon.com/polly/latest/dg/examples-java.html

Can someone help fix my code? What do I change in my code?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值