首先,添加DotNetSpeech.dll引用,可以在这里直接下载,也可以参照这篇文章说的,在安装Speech SDK以后,在Common Files\Microsoft Shared\Speech\目录下面找到SAPI.dll,用Tlbimp.exe工具将该dll转换成.net平台下的Assembly---DotNetSpeech.dll。
然后,using DotNetSpeech;
朗读功能:
using DotNetSpeech;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Speech.Synthesis;
using System.Speech;namespace MyProject
{public class ValuesController : ApiController{public SpeechSynthesizer synth; //语音合成对象GET api/<controller>//public IEnumerable<string> Get()//{// return new string[] { "value1", "value2" };//}GET api/<controller>/5//public string Get(string cont)//{// return cont;//}POST api/<controller>//public void Post([FromBody] string value)//{//}PUT api/<controller>/5//public void Put(int id, [FromBody] string value)//{//}DELETE api/<controller>/5//public void Delete(int id)//{//}//[HttpPost][HttpGet]public string ToCall(string cont){//调用示例:http://192.168.6.195:8081/api/values/ToCall?cont=请,刘笑笑,李秀秀,导医台领结果吧//https://localhost:44399/api/values/ToCall?cont=请,刘笑笑,李秀秀,导医台领结果吧//SpeechVoiceSpeakFlags flags = SpeechVoiceSpeakFlags.SVSFlagsAsync;//SpVoice sp = new SpVoice();sp.Voice = sp.GetVoices(" name=Microsoft Simplified Chinese ", "").Item(0);//sp.Voice = sp.GetVoices(string.Empty, string.Empty).Item(0); //0选择默认的语音,//sp.Rate = 0;//语速//sp.Volume = 100;//音量//sp.Speak(cont, flags);synth = new SpeechSynthesizer();//使用 synth 设置朗读音量 [范围 0 ~ 100]synth.Volume = 100;//使用 synth 设置朗读频率 [范围 -10 ~ 10]synth.Rate = 0;synth.SelectVoice(synth.GetInstalledVoices()[0].VoiceInfo.Name);//synth.SelectVoice("Microsoft Lili");//Voice.Speak(ggg, SpFlags);synth.SpeakAsync(cont);return "12345";}[HttpGet]public string GetAll(){return "Success";}}
}
注:SpeechVoiceSpeakFlags是语音朗读的风格; Voice中是语音类型(语言、男(女)声),有 Microsoft Simplified Chinese,Microsoft Mary(Sam,Mike)等,
也可以这样:voice.Voice = voice.GetVoices(string.Empty, string.Empty).Item(0); //0选择默认的语音,
1选择第二个语音;Rate指的是语速。
当然,你也可以在此不写,打开控制面板中的语音设置类型和语速
生成语音文件:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Speech.Synthesis;
using System.Collections.ObjectModel;
using System.Web.Configuration;
using DotNetSpeech;
using Microsoft.Win32;
using System.Windows.Forms;
using System.Threading;SpeechVoiceSpeakFlags flags = SpeechVoiceSpeakFlags.SVSFlagsAsync;SpVoice sp = new SpVoice();//sp.Voice = sp.GetVoices(" name=Microsoft Simplified Chinese ", "").Item(0);sp.Voice = sp.GetVoices(string.Empty, string.Empty).Item(0); //0选择默认的语音,sp.Rate = 0;//语速sp.Speak(strCont, flags);System.Windows.Forms.SaveFileDialog dialog = new System.Windows.Forms.SaveFileDialog();dialog.Filter = " All files (*.*)|*.*|wav files (*.wav)|*.wav ";dialog.Title = " Save to a wave file ";dialog.FilterIndex = 2;dialog.RestoreDirectory = true;if (dialog.ShowDialog() == DialogResult.OK){SpeechStreamFileMode spFileMode = SpeechStreamFileMode.SSFMCreateForWrite;SpFileStream spFileStream = new SpFileStream();spFileStream.Open(dialog.FileName, spFileMode, false);sp.AudioOutputStream = spFileStream;sp.Speak("文字转语音的内容", flags);sp.WaitUntilDone(Timeout.Infinite);spFileStream.Close();}
(在WinForm和Web中都适用)
参考:http://www.microsoft.com/china/community/program/originalarticles/TechDoc/Cnspeech.mspx
使用语音即时校对输入内容 - 斯克迪亚 - 博客园
转载于:https://www.cnblogs.com/pfs1314/archive/2011/01/11/1932870.html