C# 使用System.Speech 进行语音播报和识别

  using System.Speech.Synthesis;
using System.Speech.Recognition; //语音识别
SpeechRecognitionEngine recEngine = new SpeechRecognitionEngine();
SpeechSynthesizer speech = new SpeechSynthesizer(); //**************************使用System.Speech 制作文字转换成声音的程序*******************************
//rate: 范围为:-10~10;
//volume: 范围为:0~100;
//speektext: 待转换声音的文字
public void SpeechVideo_Read(int rate, int volume, string speektext) //读
{
speech.Rate = rate;
speech.Volume = volume;
speech.SpeakAsync(speektext);
} public void SpeechVideo_Record(int rate, int volume, string recordtext) //录音
{
SpeechSynthesizer speech = new SpeechSynthesizer();
speech.Rate = rate;
speech.Volume = volume;
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "文本文件(*.wav)|*.wav|所有文件(*.*)|*.*";
//设置默认文件类型显示顺序
sfd.FilterIndex = ;
//保存对话框是否记忆上次打开的目录
sfd.RestoreDirectory = true;
if (sfd.ShowDialog() == DialogResult.OK)
{
string localfilepath = sfd.FileName.ToString();
speech.SetOutputToWaveFile(localfilepath);
speech.Speak(recordtext);
speech.SetOutputToDefaultAudioDevice();
MessageBox.Show("完成录音!", "提示");
}
} public void SpeechVideo_Pause() //暂停
{
speech.Pause();
} public void SpeechVideo_Contine() //暂停后继续
{
speech.Resume();
}
//**************************************结束******************************************************** //*****************************************使用System.Speech 制作语音识别程序***********************
//rate: 范围为:-10~10;
//volume: 范围为:0~100;
//speektext: 待转换声音的文字
public void recEngine_Speech_RecordCheck() //读
{
Choices preCmd = new Choices();
preCmd.Add(new string[] { "name", "age" });
GrammarBuilder gb = new GrammarBuilder();
gb.Append(preCmd);
Grammar gr = new Grammar(gb);
recEngine.LoadGrammarAsync(gr);
recEngine.SetInputToDefaultAudioDevice();
recEngine.RecognizeAsync(RecognizeMode.Multiple);
recEngine.SpeechRecognized += recEngine_SpeechRecognized;
} public void recEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
switch (e.Result.Text)
{
case "name":
MessageBox.Show("wangjin");
break;
case "age":
MessageBox.Show("");
break;
}
} //**************************************结束****************************************************//
05-02 07:44