First, install the SpeechSDK5.1 Development Kit and the SpeechSDK5.1 Langague Pack (both Chinese and English) Language Pack. However, VS2010 is compatible with the com component that comes with SpeechSDK5.0.
Here are four methods:
When reading aloud, use
Copy codeThe Code is as follows: voice. Speak (string, SpeechVoiceSpeakFlags. SVSFlagsAsync );
Pause and useCopy codeThe Code is as follows: voice. Pause ();
Continue reading from the pause. UseCopy codeThe Code is as follows: voice. Resume ();
Stop FunctionCopy codeThe Code is as follows: voice. Speak (string. Empty, SpeechVoiceSpeakFlags. SVSFPurgeBeforeSpeak );
In this way, the functions of "read aloud", "pause", "continue", and "stop" can be fully realized.
The following example code is provided:
Copy codeThe Code is as follows: private void button#click (object sender, EventArgs e)
{
Analyse (this. textBox1.Text );
}
Public void Analyse (string strSpeak)
{
Int iCbeg = 0;
Int iEbeg = 0;
Bool IsChina = true;
For (int I = 0; I <strSpeak. Length; I ++)
{
Char chr = strSpeak [I];
If (IsChina)
{
If (Convert. ToInt32 (chr) <= 122 & Convert. ToInt32 (chr)> = 65)
{
Int iLen = I-iCbeg;
String strValue =
StrSpeak. Substring (iCbeg, iLen );
SpeakChina (strValue );
IEbeg = I;
IsChina = false;
}
}
Else
{
If (Convert. ToInt32 (chr)> 122 | Convert. ToInt32 (chr) <65)
{
Int iLen = I-iEbeg;
String strValue =
StrSpeak. Substring (iEbeg, iLen );
This. SpeakEnglishi (strValue );
ICbeg = I;
IsChina = true;
}
}
}
If (IsChina)
{Int iLen = strSpeak. Length-iCbeg;
String strValue = strSpeak. Substring (iCbeg, iLen );
SpeakChina (strValue );
}
Else
{
Int iLen = strSpeak. Length-iEbeg;
String strValue = strSpeak. Substring (iEbeg, iLen );
SpeakEnglishi (strValue );
}
}
// Chinese
Private void SpeakChina (string speak)
{
Voice = new SpVoice ();
Voice. Voice = voice. GetVoices (string. Empty, string. Empty). Item (3); // where 3 is Chinese and 024 is English
Voice. Speak (speak, SpeechVoiceSpeakFlags. SVSFDefault );
}
// English
Private void SpeakEnglishi (string speak)
{
Voice = new SpVoice ();
Voice. Voice = voice. GetVoices (string. Empty, string. Empty). Item (0); // where 3 is Chinese and 024 is English
Voice. Speak (speak, SpeechVoiceSpeakFlags. SVSFDefault );
}
// Save the voice
Private void button2_Click (object sender, EventArgs e)
{
Try
{
SpeechVoiceSpeakFlags SpFlags = SpeechVoiceSpeakFlags. SVSFlagsAsync;
SpVoice Voice = new SpVoice ();
SaveFileDialog sfd = new SaveFileDialog ();
Sfd. Filter = "All files (*. *) | *. * | wav files (*. wav) | *. wav ";
Sfd. Title = "Save to a wave file ";
Sfd. FilterIndex = 2;
Sfd. RestoreDirectory = true;
If (sfd. ShowDialog () = DialogResult. OK)
{
SpeechStreamFileMode SpFileMode = SpeechStreamFileMode. SSFMCreateForWrite;
SpFileStream = new SpFileStream ();
SpFileStream. Open (sfd. FileName, SpFileMode, false );
Voice. AudioOutputStream = SpFileStream;
Voice. Speak (this. textBox1.Text, SpFlags );
Voice. WaitUntilDone (100 );
SpFileStream. Close ();
}
}
Catch (Exception er)
{
MessageBox. Show ("An Error Occured! "," SpeechApp ", MessageBoxButtons. OK, MessageBoxIcon. Error );
}
}