我正在尝试将 AWS Polly 用于我的简单 JAVA 控制台应用程序的文本到语音功能。我从 AWS 获得结果,但是一旦我尝试使用 AudioStreamPlayer 播放它(可以在这里找到课程:http: //blog.conygre.com/2016/12/06/at-the-third-stroke-the-time-will -be-spoken-by-aws-polly/)我收到一个错误。
似乎我从 AWS 获得的结果格式是 MPEG,而不是要求的 MP3。
我的代码:
BasicAWSCredentials awsCreds = new BasicAWSCredentials(key, secret);
System.out.println(System.getenv("AWS_REGION"));
AmazonPollyClient apClient = (AmazonPollyClient) AmazonPollyClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(awsCreds))
.build();
SynthesizeSpeechRequest tssRequest = new SynthesizeSpeechRequest();
tssRequest.setText("Hello World");
tssRequest.setVoiceId("Ruben");
tssRequest.setOutputFormat(OutputFormat.Mp3);
SynthesizeSpeechResult tssResult = apClient.synthesizeSpeech(tssRequest);
System.out.println(tssResult.getContentType());
InputStream in = tssResult.getAudioStream();
BufferedInputStream bin = new BufferedInputStream(in);
AudioStreamPlayer player = new AudioStreamPlayer();
player.play(bin);
控制台输出:
eu-west-1
audio/mpeg
Exception in thread "main" java.lang.IllegalStateException: javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input stream
at AudioStreamPlayer.play(AudioStreamPlayer.java:35)
at PollyTest.main(PollyTest.java:31)
Caused by: javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input stream
at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1121)
at AudioStreamPlayer.play(AudioStreamPlayer.java:16)
... 1 more