Amitesh
Amitesh

Reputation: 677

ASP.NEt MVC Text to Speech

We have a MVC site and now we want to add a functionality where we want to ability to read out text on client side. We display a set of invoices and we want to read out the invoice details. I know you can use System.Speech library with desktop applications to achieve this by pass the details to be read out but does anybody know how can we do this in the MVC project.

Upvotes: 2

Views: 3183

Answers (2)

Michael Aguilar
Michael Aguilar

Reputation: 856

I was researching and I found this way, its similar to John Kalberer's answer:

public async Task<ActionResult> Index()
    {
        Task<FileContentResult> task = Task.Run(() =>
        {
            using (var synth = new SpeechSynthesizer())
            {
                synth.SelectVoice("Microsoft Sabina Desktop");

                using (var stream = new MemoryStream())
                {
                    synth.SetOutputToWaveStream(stream);
                    synth.Speak("hola mundo");
                    byte[] bytes = stream.GetBuffer();
                    return File(bytes, "audio/x-wav");
                }
            }
        });

        return await task;
    }

In this case I'm using the spanish voice, but of course you can select your voice or avoid the synth.SelectVoice and use the predeterminated

Upvotes: 0

John Kalberer
John Kalberer

Reputation: 5790

Well, this is just off the top of my head and it hasn't been tested but you may be able to do something like this:

public ActionResult Speak(string text)
{
    var speech = new SpeechSynthesizer();
    speech.Speak(text);

    byte[] bytes;
    using (var stream = new MemoryStream())
    {
        speech.SetOutputToWaveStream(stream);
        bytes = stream.ToArray();
    }
    return File(bytes, "audio/x-wav");
}

Upvotes: 2

Related Questions