0

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>
Kabilesh
  • 1,000
  • 6
  • 22
  • 47
  • 1
    This is because your browser is caching the file. Your should add a dummy parameter in the query string which change each request. – ADreNaLiNe-DJ Nov 21 '17 at 08:32
  • Well, you should know what "browser caching" is - because browser tends to cache file once it's loaded. You can set a viewmodel with parameter to differentiate each speech request or using partial view to load audio file for each POST request. – Tetsuya Yamamoto Nov 21 '17 at 08:34
  • Thanks. I will work on that. – Kabilesh Nov 21 '17 at 08:41
  • You can try with meta tag: `` or `[OutputCache(Duration = 0, NoStore = true)]` attribute in `TTS` action method. – Tetsuya Yamamoto Nov 21 '17 at 08:44

0 Answers0