4
我正在嘗試輸出文本到語音的wav文件,並使用HTML5 <audio>
標記進行播放。文本到語音方法正在輸出字節,但html5控件不會播放它。使用HTML5播放wav
如果不是直接將字節流式傳輸到控件,我首先將它保存爲文件,然後將文件轉換爲帶有文件流的字節並輸出,然後開始播放,但我不想保存該文件每次。我正在使用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>