到HTTP无响应获取请求的WebAPI在.NET 4.5,而使用语音合成转换文本到语音 [英] No response to a HTTP Get request in WebAPI in .NET 4.5 while using SpeechSynthesis for converting text to speech
问题描述
我试图安装使用的WebAPI一个简单的Web服务。以下是我为code:
I'm trying to setup a simple web service using WebAPI. Here is what I have for code:
public class SpeakController : ApiController
{
//
// api/speak
public HttpResponseMessage Get(String textToConvert, String outputFile, string gender, string age = "Adult")
{
VoiceGender voiceGender = (VoiceGender)Enum.Parse(typeof(VoiceGender), gender);
VoiceAge voiceAge = (VoiceAge)Enum.Parse(typeof(VoiceAge), age);
using (SpeechSynthesizer synthesizer = new SpeechSynthesizer())
{
synthesizer.SelectVoiceByHints(voiceGender, voiceAge);
synthesizer.SetOutputToWaveFile(outputFile, new SpeechAudioFormatInfo(8000, AudioBitsPerSample.Sixteen, AudioChannel.Mono));
synthesizer.Speak(textToConvert);
}
return Request.CreateResponse(HttpStatusCode.OK, new Response { HttpStatusCode = (int)HttpStatusCode.OK, Message = "Payload Accepted." });
}
}
在code是相当简单的,它绝不是生产做好准备。但在我的测试中,我已经注意到了发生以下情况提出任何要求到控制器:
The code is fairly straight forward and it is by no means production ready. But in my tests I have noticed the following occurs for any request to the controller:
- 的WAV文件被成功生成
- 调试过程中,我可以看到控制敲回车键,退出该方法
- 然而,我的浏览器只是不断旋转,我从来没有从服务器 获得回应
我试图与邮差(REST客户端的浏览器)一样,得到了相同的结果。虽然我不希望这是一个阻塞调用,在尝试其他事情,我修改了兴趣 synthesizer.Speak
到 synthesizer.SpeakAsync
和遇到同样的问题。
I tried the same with Postman (a REST client for Chrome) and got the same result. Though I do want this to be a blocking call, in the interest of trying other things I modified synthesizer.Speak
to synthesizer.SpeakAsync
and encountered the same issue.
然而,当我单独测试片段如下图所示,在code正常工作。
However when I test the snippets separately as shown below, the code works as expected.
测试的WebAPI的呼叫与语音部注释掉:
public class SpeakController : ApiController
{
//
// api/speak
public HttpResponseMessage Get(String textToConvert, String outputFile, string gender, string age = "Adult")
{
VoiceGender voiceGender = (VoiceGender)Enum.Parse(typeof(VoiceGender), gender);
VoiceAge voiceAge = (VoiceAge)Enum.Parse(typeof(VoiceAge), age);
//using (SpeechSynthesizer synthesizer = new SpeechSynthesizer())
//{
// synthesizer.SelectVoiceByHints(voiceGender, voiceAge);
// synthesizer.SetOutputToWaveFile(outputFile, new SpeechAudioFormatInfo(8000, AudioBitsPerSample.Sixteen, AudioChannel.Mono));
// synthesizer.Speak(textToConvert);
//}
return Request.CreateResponse(HttpStatusCode.OK, new Response { HttpStatusCode = (int)HttpStatusCode.OK, Message = "Payload Accepted." });
}
}
单独测试的讲话在控制台应用程序:
static string usageInfo = "Invalid or no input arguments!"
+ "\n\nUsage: initiatives \"text to speak\" c:\\path\\to\\generate.wav gender"
+ "\nGender:\n\tMale or \n\tFemale"
+ "\n";
static void Main(string[] args)
{
if (args.Length != 3)
{
Console.WriteLine(usageInfo);
}
else
{
ConvertStringToSpeechWav(args[0], args[1], (VoiceGender)Enum.Parse(typeof(VoiceGender), args[2]));
}
Console.WriteLine("Press any key to continue...");
Console.ReadLine();
}
static void ConvertStringToSpeechWav(String textToConvert, String pathToCreateWavFile, VoiceGender gender, VoiceAge age = VoiceAge.Adult)
{
using (SpeechSynthesizer synthesizer = new SpeechSynthesizer())
{
synthesizer.SelectVoiceByHints(gender, age);
synthesizer.SetOutputToWaveFile(pathToCreateWavFile, new SpeechAudioFormatInfo(8000, AudioBitsPerSample.Sixteen, AudioChannel.Mono));
synthesizer.Speak(textToConvert);
}
}
的WebAPI和语音合成似乎并没有发挥得很好。在弄清这一点任何帮助将大大AP preciated。
WebAPI and SpeechSynthesis do not seem to play well together. Any help in figuring this out would be greatly appreciated.
谢谢!
推荐答案
我不知道为什么发生这种情况,但在一个单独的线程中运行你的SpeechSynthesizer似乎这样的伎俩(不兼容的线程模型?)。下面是我如何在过去做到了。
I have no idea why this happens but running your SpeechSynthesizer in a separate thread seems to do the trick (incompatible threading model?). Here's how I've done it in the past.
根据<一个href=\"http://stackoverflow.com/questions/12343249/ultra-fast-text-to-speech-wav-mp3-in-asp-net-mvc\">Ultra快速文本到语音(WAV - &GT; MP3)在ASP.NET MVC
public dynamic Post(dynamic req)
{
try
{
string phrase = req["phrase"].Value;
var stream = new MemoryStream();
var t = new System.Threading.Thread(() =>
{
using (var synth = new SpeechSynthesizer())
{
synth.SetOutputToWaveStream(stream);
synth.Speak(phrase);
synth.SetOutputToNull();
}
});
t.Start();
t.Join();
stream.Position = 0;
var resp = new HttpResponseMessage(HttpStatusCode.OK);
resp.Content = new StreamContent(stream);
resp.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
resp.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
resp.Content.Headers.ContentDisposition.FileName = "phrase.wav";
return resp;
}
catch
{
return new HttpResponseMessage(HttpStatusCode.InternalServerError);
}
}
这篇关于到HTTP无响应获取请求的WebAPI在.NET 4.5,而使用语音合成转换文本到语音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!