【问题标题】:AWS Amazon Polly mp3 file creation through java通过 java 创建 AWS Amazon Polly mp3 文件
【发布时间】:2023-03-14 04:55:01
【问题描述】:

我需要使用 AEM(Java) 创建文本到语音组件,为此我正在使用 Amazon polly。 我需要使用 SDK 2.0 版本的 AWS 库。我正在发送请求中的文本并将 mp3 文件直接保存到 AWS S3 存储桶中,作为回报,我需要 mp3 文件的 url。 我找不到任何使用 polly 创建 mp3 文件并直接存储在 S3 存储桶上的示例。 谁能给我提供通过java做的例子?

【问题讨论】:

    标签: amazon-web-services amazon-s3 amazon-polly


    【解决方案1】:

    这是一个 (https://docs.aws.amazon.com/polly/latest/dg/SynthesizeSpeechSample.html)

    package com.amazonaws.polly.samples;
    
    import com.amazonaws.services.polly.AmazonPolly;
    import com.amazonaws.services.polly.AmazonPollyClientBuilder;
    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.VoiceId;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.InputStream;
    
    public class SynthesizeSpeechSample {
        AmazonPolly client = AmazonPollyClientBuilder.defaultClient();
    
        public void synthesizeSpeech() {
            String outputFileName = "/tmp/speech.mp3";
    
            SynthesizeSpeechRequest synthesizeSpeechRequest = new SynthesizeSpeechRequest()
                    .withOutputFormat(OutputFormat.Mp3)
                    .withVoiceId(VoiceId.Joanna)
                    .withText("This is a sample text to be synthesized.");
    
            try (FileOutputStream outputStream = new FileOutputStream(new File(outputFileName))) {
                SynthesizeSpeechResult synthesizeSpeechResult = client.synthesizeSpeech(synthesizeSpeechRequest);
                byte[] buffer = new byte[2 * 1024];
                int readBytes;
    
                try (InputStream in = synthesizeSpeechResult.getAudioStream()){
                    while ((readBytes = in.read(buffer)) > 0) {
                        outputStream.write(buffer, 0, readBytes);
                    }
                }
            } catch (Exception e) {
                System.err.println("Exception caught: " + e);
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 2013-04-17
      • 2019-11-16
      • 1970-01-01
      • 1970-01-01
      • 2018-12-13
      • 2023-01-07
      相关资源
      最近更新 更多