语音服务器 - 将wav作为字节数组返回的WCF服务

时间:2016-07-07 18:30:10

标签: c# wcf

我制作了一个WCF,以便充当语音服务器。

我的合同:

namespace RybenaTTSWS
{
    [ServiceContract]
    public interface IRybenaTTS
    {
        [OperationContract]
        [WebGet(UriTemplate="{text}",BodyStyle=WebMessageBodyStyle.WrappedRequest)]
        byte[] Speak(string text);
    }
}

我的实施:

namespace RybenaTTSWS
{
    public class RybenaTTS : IRybenaTTS
    {
        public byte[] Speak(string text)
        {
            OutgoingWebResponseContext context = WebOperationContext.Current.OutgoingResponse;

            SpeechSynthesizer synth = new SpeechSynthesizer();

            MemoryStream ms = new MemoryStream();

            synth.Rate = 1;
            synth.Volume = 100;
            synth.SelectVoice("Microsoft Maria Desktop");
            synth.SetOutputToWaveStream(ms);

            synth.Speak(text);

            synth.SetOutputToNull();

            context.Headers.Add(System.Net.HttpResponseHeader.CacheControl, "public");
            context.ContentType = "audio/wav";
            context.StatusCode = System.Net.HttpStatusCode.OK;

            return ms.GetBuffer();
        }
    }
}

我可以使用这个WCF,但是我想直接将其称为客户端HTML上的音频标记的src。问题是返回一些我认为它不是二进制数组的东西。

例如,当我通过浏览器将其称为

http://172.16.0.107/RybenaTTSWS/RybenaTTS.svc/Speak?text=sometext 
在Microsoft Edge上

,它返回

<base64Binary xmlns="http://schemas.microsoft.com/2003/10/Serialization/">UklGRoTWAABXQVZFZ.....=</base64Binary>

在Chrome上,它会返回&#34;没有&#34;,这样说;只是一个空白页面(在开发者工具上说我Resource interpreted as Document but transferred with MIME type audio/wav)。

我错过了什么?

2 个答案:

答案 0 :(得分:1)

以下是Google Chrome和Firefox中的工作示例。在IE中我得到异常“System.ArgumentException:指定的值具有无效的控制字符。参数名称:值。”。在我看来,IE在HTTP请求中使用了一些奇怪的参数。

HTML:

<!DOCTYPE html>
<html>
<body>
<audio src="http://localhost:8000/WaveService/GetWave" autoplay>
  Your browser does not support the <code>audio</code> element.
</audio>
</body>
</html>

C#代码:

namespace WaveSampe
{
    using System;
    using System.IO;
    using System.ServiceModel;
    using System.ServiceModel.Description;
    using System.ServiceModel.Web;

    [ServiceContract]
    public interface IWaveService
    {
        [WebGet]
        Stream GetWave();
    }
    public class WaveService : IWaveService
    {
        public Stream GetWave()
        {
            var resp = WebOperationContext.Current.OutgoingResponse;
            resp.ContentType = "audio/wav";
            // Clear caches
            resp.Headers.Add(System.Net.HttpResponseHeader.CacheControl, "no-cache");
            resp.Headers.Add(System.Net.HttpResponseHeader.Pragma, "no-cache");
            resp.Headers.Add(System.Net.HttpResponseHeader.Expires, "0");

            // Wave example. Source is http://www-mmsp.ece.mcgill.ca/documents/audioformats/wave/Samples/AFsp/M1F1-Alaw-AFsp.wav
            return new FileStream("example.wav", FileMode.Open, FileAccess.Read, FileShare.Read);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            string baseAddress = "http://localhost:8000/WaveService";
            ServiceHost host = new ServiceHost(typeof(WaveService), new Uri(baseAddress));
            host.AddServiceEndpoint(typeof(IWaveService), new WebHttpBinding(), "").Behaviors.Add(new WebHttpBehavior());
            host.Open();
            Console.WriteLine("Service is running");
            Console.Write("Press ENTER to close the host");
            Console.ReadLine();
            host.Close();
        }
    }
}

答案 1 :(得分:0)

好的,我已经开始工作......

返回一个Stream,但不要忘记在返回之前将Stream的位置设置为0!

我真的很想知道原因,但无论如何......工作了!

    public Stream Speak(string text)
    {
        OutgoingWebResponseContext context = WebOperationContext.Current.OutgoingResponse;

        SpeechSynthesizer synth = new SpeechSynthesizer();

        MemoryStream ms = new MemoryStream();

        synth.Rate = 1;
        synth.Volume = 100;
        synth.SelectVoice("Microsoft Maria Desktop");
        synth.SetOutputToWaveStream(ms);

        synth.Speak(text);

        synth.SetOutputToNull();

        context.Headers.Add(System.Net.HttpResponseHeader.CacheControl, "public");
        context.ContentType = "audio/wav";
        context.StatusCode = System.Net.HttpStatusCode.OK;


        ms.Position = 0;    // <<<------------ That's the secret!!!

        return ms;
    }
相关问题