微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

亚马逊 Polly 不说话

如何解决亚马逊 Polly 不说话

环境

  • Angular11.2
  • Spring Boot2.4.5
  • 带有 snd-aloop 的 EC2(Ubuntu20.04)

我基于 AWS sdk for java 2.0 实现了 Amazon Polly。它在本地环境中说话,但在生产(EC2)中虽然没有发生错误,但它却没有。来源如下:

    @GetMapping("/sound/{chinese}")
    public void getSound(@PathVariable("chinese") String chinese) throws IOException {
       
        ResourceBundle rb = ResourceBundle.getBundle(RESOURCE_NAME);
        final String accessKey = rb.getString("pollyAccessKey");
        final String secretKey = rb.getString("pollySecretKey");
        AwsBasicCredentials awsCreds = AwsBasicCredentials.create(accessKey,secretKey);
        final String REGION = rb.getString("region");

        try {
            PollyClient polly = PollyClient.builder()
                .credentialsProvider(StaticCredentialsProvider.create(awsCreds))
                .region(Region.of(REGION))
                .build();

            DescribeVoicesRequest describeVoiceRequest = DescribeVoicesRequest.builder()
                    .languageCode("cmn-CN")
                    .engine("standard")
                    .build();

            DescribeVoicesResponse describeVoicesResult = polly.describeVoices(describeVoiceRequest);
            Voice voice = describeVoicesResult.voices().get(0);
            InputStream stream = synthesize(polly,chinese,voice,OutputFormat.MP3);  
            Advancedplayer player = new Advancedplayer(stream,javazoom.jl.player.FactoryRegistry.systemRegistry().createAudioDevice());
            player.play();
            polly.close();
        } catch (PollyException | JavaLayerException | IOException e) {
            System.out.println(e.getStackTrace());
            System.exit(1);
        }
    }


 public static InputStream synthesize(PollyClient polly,String text,Voice voice,OutputFormat format) throws IOException {
        SynthesizeSpeechRequest synthReq = SynthesizeSpeechRequest.builder()
                .text(text)
                .voiceId(voice.id())
                .outputFormat(format)
                .build();

        ResponseInputStream<SynthesizeSpeechResponse> synthRes = polly.synthesizeSpeech(synthReq);
        return synthRes;
    }

我花了大约一周的时间试图解决这个问题,但它不起作用。 我不知道了。提前致谢。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。