I have implemented text to speech conversion in asp.net with the help of my question in How to use windows speech synthesizer in ASP.NET MVC.
The text is converted to speech and stored in a file Voicespeech.wav. The browser plays the voice. Once the application is stared, I enter text and submit and it is spoken. But for every next text also the browser speaks the same first text. But when I checked the voice file with windows media player, the voice had been replaced with new text. How can I make it speak the new text entered?
Also every time I refresh the browser, it plays the voice. I want it to be played only for the button click. And each time, the text entered in the input box should be played, not first entered text after starting debugging.
I hope the question is understandable. Code is below.
ApplicationController.cs
[HttpPost]
public async Task<ActionResult> TTS(string text)
{
// you can set output file name as method argument or generated from text
string speech = "speech";
Task<ViewResult> task = Task.Run(() =>
{
using (SpeechSynthesizer speechSynthesizer = new SpeechSynthesizer())
{
speechSynthesizer.SetOutputToWaveFile(Server.MapPath("~/Voice/Voice") + speech + ".wav");
speechSynthesizer.Speak(text);
ViewBag.FileName = speech + ".wav";
return View();
}
});
return await task;
}
TTS.cshtml
<div>
<form action="~/Application/TTS" method="post">
<div class="form-group">
<label for="email">Enter Text</label>
<input type="text" name="text" class="form-control" id="searchbar">
</div>
<button type="submit" class="btn btn-default">Speak</button>
</form>
</div>
I believe the fault is here.
<div>
<audio autoplay="autoplay" src="@Url.Content("~/Voice/Voice" + ViewBag.FileName)"></audio>
</div>