在进行 Amazon Polly 文本到语音转换时接收零字节音频流
Receiving zero byte audio stream when doing Amazon Polly text to speech conversions
我正在尝试使用 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
响应(没有抛出异常)但是音频流是空的:
缺少什么?
返回的 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();
}
}
}
我相信你所要做的就是在保存之前刷新流(甚至查看长度)
response.AudioStream.CopyTo(destination);
destination.Flush();
看看这是否适合您。
我正在尝试使用 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
响应(没有抛出异常)但是音频流是空的:
缺少什么?
返回的 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();
}
}
}
我相信你所要做的就是在保存之前刷新流(甚至查看长度)
response.AudioStream.CopyTo(destination);
destination.Flush();
看看这是否适合您。