【问题标题】:Receiving zero byte audio stream when doing Amazon Polly text to speech conversions在执行 Amazon Polly 文本到语音转换时接收零字节音频流
【发布时间】:2017-01-13 01:58:57
【问题描述】:

我正在尝试使用 Amazon Web Services Polly 和适用于 C# 的 AWS 开发工具包进行文本到语音的转换。我尝试了一个非常基本的转换:

AmazonPollyClient client = new AmazonPollyClient("secret", "secret", Amazon.RegionEndpoint.USEast1);
Amazon.Polly.Model.SynthesizeSpeechRequest request = new SynthesizeSpeechRequest();
request.OutputFormat = OutputFormat.Mp3;
request.Text = "This is my first conversion";
request.TextType = TextType.Text;
request.VoiceId = VoiceId.Nicole;
Amazon.Polly.Model.SynthesizeSpeechResponse response = client.SynthesizeSpeech(request);

我收到了一个HTTP 200 OK 响应(没有抛出异常)但是音频流是空的:

缺少什么?

【问题讨论】:

    标签: c# .net amazon-web-services text-to-speech amazon-polly


    【解决方案1】:

    返回的AudioStream 没有长度,直到您在某处读取它,例如读入文件:

    using System;
    using System.IO;
    using Amazon;
    using Amazon.Polly;
    using Amazon.Polly.Model;
    namespace AwsPollySO1
    {
        class Program
        {
            public static void Main(string[] args)
            {
                AmazonPollyClient client = new AmazonPollyClient("yourID", "yourSecretKey", RegionEndpoint.USEast1);
                SynthesizeSpeechRequest request = new SynthesizeSpeechRequest();
                request.OutputFormat = OutputFormat.Mp3;
                request.Text = "This is my first conversion";
                request.TextType = TextType.Text;
                request.VoiceId = VoiceId.Nicole;
                SynthesizeSpeechResponse response = client.SynthesizeSpeech(request);
                Console.WriteLine("ContentType: " + response.ContentType);
                Console.WriteLine("RequestCharacters: " + response.RequestCharacters);
                FileStream destination = File.Open(@"c:\temp\myfirstconversion.mp3", FileMode.Create);
                response.AudioStream.CopyTo(destination);
                Console.WriteLine("Destination length: {0}", destination.Length.ToString());
                destination.Close();
                Console.Read();
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      我相信您所要做的就是在保存(甚至查看长度)之前刷新流

      response.AudioStream.CopyTo(destination);
      destination.Flush();
      

      看看这是否对你有用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-08-12
        • 2018-01-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-14
        • 2018-10-27
        相关资源
        最近更新 更多