I am trying to put the text into the speech of a wav file and play it with the HTML5 <audio>
. The text-to-speech method outputs bytes, but the html5 control does not play it.
If instead of streaming bytes directly to the control, I first save it as a file, and then convert the file to bytes with a file stream and output it, it starts to play, but I do not want to save the file every time. I am using MVC 4.
// in a class library public byte[] GenerateAudio(string randomText) { MemoryStream wavAudioStream = new MemoryStream(); SpeechSynthesizer speechEngine = new SpeechSynthesizer(); speechEngine.SetOutputToWaveStream(wavAudioStream); speechEngine.Speak(randomText); wavAudioStream.Flush(); Byte[] wavBytes = wavAudioStream.GetBuffer(); return wavBytes; } // in my controller public ActionResult Listen() { return new FileContentResult(c.GenerateAudio(Session["RandomText"].ToString()), "audio/wav"); } // in my view <audio controls autoplay> <source src="@Url.Content("~/Captcha/Listen")" type="audio/wav" /> Your browser does not support the <audio> element. </audio>
source share