Click here to Skip to main content
15,867,686 members
Articles / Programming Languages / C#
Tip/Trick

Use Speech Reference in Visual C#.NET

Rate me:
Please Sign up or sign in to vote.
4.75/5 (8 votes)
5 Mar 2011CPOL1 min read 28.1K   5   12
Develop an application in Visual C#.NET called "Understand and Read It!".
Understand and Read It! – Visual C# API

Hi Everybody,

Here, we will develop an application in Visual C# called "Understand and Read It!". Mohammad Elsheimy's blog is the source of this article. Why? Because, Mohammad Elsheimy has already written it. It is in here (Building Applications that Can Talk). So special thanks to Mohammed Elsheimy.

What is this about?

The Speech API library that we are going to use today is represented by the file sapi.dll which is located in %windir%\System32\Speech\Common. This library is not part of the .NET BCL and it’s not even a .NET library, so we’ll use interoperability to communicate with it (don't worry, when using Visual Studio, it is just a matter of adding a reference to the application)

Here We Go!

Now, in this step, we add a reference of the Speech Library to the project and design our Windows form. Look at the following figure 1:

Figure 1

After this process comes step 2. Design our form as in figure 2:

Figure 2

As shown in the form, I have got one Label, two Buttons and one TextBox. Now, we go to the third and final step!

Write Code For Yourself!

This is the final step. Write code for ourselves as shown in figure 3:

Figure 3
private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                string msgeror = "Text box must not null! Please try again.";
                MessageBox.Show(msgeror);
                SpVoice erspeak = new SpVoiceClass();
                erspeak.Speak(msgeror, SpeechVoiceSpeakFlags.SVSFDefault);
            }
            else
            {
                SpVoice speak = new SpVoiceClass();
                speak.Speak(textBox1.Text, SpeechVoiceSpeakFlags.SVSFDefault);
            }
        }

Notes

If you are using Visual Studio 2010 and .NET 4 Framework, the application will fail to run because of Interop problems. So you must select "false" to Embed Interop Types property of Speech Reference.

Download

  • To download the Word document file of this article, please Click here.
  • For download Visual Studio project file, please Click here.

Special Thanks

Thanks to Mohammad Elsheimy who inspired me. Also, if you want to visit the "Building Applications that Can Talk" article, please click here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Turkey Turkey
Hello there, i'm Serhat. What if it happens?

Comments and Discussions

 
GeneralRe: welcome & keep it up, BTW Come with more work man, cheers. :... Pin
thatraja6-Mar-11 6:31
professionalthatraja6-Mar-11 6:31 
welcome & keep it up, BTW Come with more work man, cheers. Smile | :)
GeneralRe: Well done man :) thank you for suggestion and vote to me :) Pin
hepicoder5-Mar-11 10:26
hepicoder5-Mar-11 10:26 
GeneralRe: Well done man. BTW you have changed some of things after my ... Pin
thatraja5-Mar-11 7:31
professionalthatraja5-Mar-11 7:31 
GeneralRe: This link have bin folder of project. Download it and try ag... Pin
hepicoder5-Mar-11 3:24
hepicoder5-Mar-11 3:24 
GeneralRe: Oh man, I said it's working, the only minor issue is the <b>... Pin
thatraja5-Mar-11 3:16
professionalthatraja5-Mar-11 3:16 
GeneralRe: I think, you must check your References so you must be sure ... Pin
hepicoder5-Mar-11 3:13
hepicoder5-Mar-11 3:13 
GeneralRe: I'm sorry man, again I need to give another minor suggestion... Pin
thatraja5-Mar-11 2:30
professionalthatraja5-Mar-11 2:30 
GeneralReason for my vote of 5 Just what I was looking for: a simpl... Pin
Hezek1ah8-Mar-11 9:57
Hezek1ah8-Mar-11 9:57 
GeneralReason for my vote of 5 You Deserve the full!! Pin
GPUToaster™6-Mar-11 19:02
GPUToaster™6-Mar-11 19:02 
GeneralRe: Thank you man. Pin
hepicoder6-Mar-11 22:43
hepicoder6-Mar-11 22:43 
General<b>Couple of suggestions</b> #Replace the <b>Write Code For ... Pin
thatraja3-Mar-11 0:47
professionalthatraja3-Mar-11 0:47 
GeneralRe: Thank you for suggestions thatraja. At first i added codes f... Pin
hepicoder3-Mar-11 1:33
hepicoder3-Mar-11 1:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.