2017-01-13 23 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(); 

看看這是否適合你。