先添加SpeechLib的引用,在系统盘Program Files\Common Files\Microsoft Shared\Speech目录中找到sapi.dll。
在代码中调用SpVoiceClass类的Speak方法可以实现简单的句子发言:
ISpeechVoice v=new SpVoiceClass();
v.Speak(txtSpeak, SpeechVoiceSpeakFlags.SVSFDefault);
v.Speak(txtSpeak, SpeechVoiceSpeakFlags.SVSFDefault);
//Declare the SpVoice object.
ISpeechVoice v=new SpVoiceClass();
//Create a wave stream
SpFileStream s = new SpFileStream();
//Create a new .wav file for writing.
s.Open("out.wav", SpeechStreamFileMode.SSFMCreateForWrite, false);
//Set the .wav file stream as the output for the Voice object
v.AudioOutputStream = s;
//Call the Speak method now will send the output to the .wav file.
v.Speak(txtSpeak, SpeechVoiceSpeakFlags.SVSFDefault);
//Close the file
s.Close();
ISpeechVoice v=new SpVoiceClass();
//Create a wave stream
SpFileStream s = new SpFileStream();
//Create a new .wav file for writing.
s.Open("out.wav", SpeechStreamFileMode.SSFMCreateForWrite, false);
//Set the .wav file stream as the output for the Voice object
v.AudioOutputStream = s;
//Call the Speak method now will send the output to the .wav file.
v.Speak(txtSpeak, SpeechVoiceSpeakFlags.SVSFDefault);
//Close the file
s.Close();