C # How to implement voice Functions

Source: Internet
Author: User

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 );
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.