The first thing to do is to install the SpeechSDK5.1 Development Kit and the SpeechSDK5.1 Langague pack (both English and Chinese), although VS2010 is a COM component with SpeechSDK5.0 in it.
A brief four methods:
When reading aloud, use
Copy Code code as follows:
Voice. Speak (String,speechvoicespeakflags.svsflagsasync);
Paused, using
Copy Code code as follows:
Continue to read aloud from the pause, using the
Copy Code code as follows:
Stop function
Copy Code code as follows:
Voice. Speak (String. Empty, Speechvoicespeakflags.svsfpurgebeforespeak);
This allows you to fully implement the "read aloud", "pause", "continue", "Stop" function.
The example code is given directly below:
Copy Code code as follows:
private void Button1_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);//3 of which 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);//3 of which is Chinese and 024 is English
Voice. Speak (Speak, Speechvoicespeakflags.svsfdefault);
}
Save speech
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 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);
}
}