2017-01-13 28 views
0

我正在尝试使用Amazon Web Services Polly和适用于C#的AWS开发工具包进行文本到语音转换。我已经尝试了很基本的转换:将Amazon Polly文本转换为语音转换时接收零字节音频流

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响应(没有抛出异常),但是音频流为空:

Empty audio stream 1 Empty audio stream 2

缺少了什么?

回答

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(); 
     } 
    } 
} 
0

我相信所有你需要做的就是冲洗,然后再储存(甚至看到长度)的流

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

看看这是否适合你。