一个简单的网页读字符串 SpeechLib

时间:2023-03-08 23:49:46
一个简单的网页读字符串 SpeechLib

//引用组件:Interop.SpeechLib.dll
//导入空间:SpeechLib

//引用组件:Interop.SpeechLib.dll
//导入空间:SpeechLib

前面设置内容引用别人博客

//1.SpVoice voice = new SpVoice();

//2.voice.Speak(txt.Text, SpeechVoiceSpeakFlags.SVSFDefault); //同步朗读

//3.voice.Speak(txt.Text, SpeechVoiceSpeakFlags.SVSFlagsAsync); //异步朗读

//voice.Voice = voice.GetVoices(string.Empty, string.Empty).Item(0);//设置中文语音
//voice.Voice = voice.GetVoices(string.Empty, string.Empty).Item(1);设置英文语音

//错误修改:VS2010中 错误无法嵌入互操作类型“SpeechLib.SpVoiceClass”。请改用适用的接口
//解决方案:选中项目中引入的dll,鼠标右键,选择属性,把“嵌入互操作类型”设置为False。

//异常来自 HRESULT:0x8004503异常信息。一般是由Windows Audio服务没有开启造成的,尤其是win2003默认不开Windows Audio服务。
//1)请确认Windows Audio(AudioSrv)服务启动。
//进入cmd 输入net start AudioSrv
//2)声卡驱动安装成功。"

功能强大之处在于TTS能识别XML标记,通过给文本加上XML标记,我们让TTS朗读出更加符合语言阅读习惯的句子。例如:
<volume level="60"></volume> 用于设置文本朗读的音量;
<rate absspeed="1"/>、<rate speed="5"/> 分别用于设置文本朗读的绝对速度和相对速度;
<pitch absmiddle="2"/>、<pitch middle="5"/> 分别用于设置文本朗读的绝对语调和相对语调;
<emph></emph> 在他们之间的句子被视为强调;
<spell></spell> 可以将单词逐个字母的拼写出来;
<silence msec="500"/> 表示停止发声,并保持500微秒;
<context id="date_mdy">02/03/07</context> 可以按要求朗读出日期
<voice required="Language=409"></voice> 用于设置朗读所用的语言,其中409表示使用英语,804表示使用汉语,而411表示日语。
后台:
   public ActionResult speak(string speechSounds)
{
SpeechVoiceSpeakFlags SpFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync;
SpVoice spVoice = new SpVoice();
spVoice.Rate = spVoice.Rate - ;
if (spVoice.Volume < )
{
spVoice.Volume = spVoice.Volume + ;
} if (spVoice.Volume > )
{
spVoice.Volume = spVoice.Volume - ;
}
spVoice.Speak("<lang langid=\"804\">" + speechSounds + "告警</lang>", SpFlags);
return Content("成功");
}

前端:

      function a() {
var url = '/Alarm/speak';
var speechSounds = "你的剑就是我的剑dfdf1234567899652554412235441235"
$.ajax({
type: "POST", url: url,
data: { speechSounds: speechSounds },
success: function (msg) { } })