Geekpedia Programming Tutorials






Speaking Computer in C#

This short tutorial will show you how to make your computer talk. This is very useful for error messages, other warnings or errors the user should be aware of.

On Thursday, May 17th 2007 at 10:07 PM
By Ryan Turney (View Profile)
*****   (Rated 4.9 with 22 votes)
Contextual Ads
More C# Resources
Advertisement
This is very simple to accomplish, we will be using Windows SAPI (Speech Application Program Interface) along with Microsoft’s Visual C# 2005 Express Edition. Our application will contain a text box that the user will be able to input text into and then push a button that will tell the application to read everything in the text box, very simple. First off, you will need to create a new Windows Project and create a textbox and a button right below it, like so. Be sure to set multiline to true when creating this.

Microsoft SAPI

Next, we need to add a reference to the SAPI. To do this, right click your project in the Solution Explorer and click on Add Reference. Once that window appears click on the COM tab, locate "Microsoft Speech Object Library" and click ok.

Microsoft Speech Object Library

Now for the final step, the code.

using System;
using System.Windows.Forms;
// You need this so you can use functions from the SAPI
using SpeechLib;
namespace SAPI
{
   public partial class Form1 : Form
   {
      public Form1()
      {
         InitializeComponent();
      }
      private void button1_Click(object sender, EventArgs e)
      {
         // Creates a new instance of SpVoice
         SpVoice voice = new SpVoice();
         // Tells the program to speak everything in the textbox using Default settings
         voice.Speak(textBox1.Text, SpeechVoiceSpeakFlags.SVSFDefault);
      }
   }
}


Now build the application and test it out, if everything was coded correctly you should hear the computer speak everything you typed into the textbox. It's really that simple! There is so much more that you can do with this, this is just to give you a little push in the right direction. Good Luck!
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this tutorial
Comment Current Comments
by Vamshi on Monday, May 28th 2007 at 01:45 AM

Thanq very much... I am beginner in C# and ur example made me sm more confident..

plz be continue with this type of examples...

- Vamshi

by Asha on Tuesday, June 19th 2007 at 02:38 AM

Hi,Thanks a lot....Its really helpful..I am a beginner in learning C#..

by Upen on Monday, July 9th 2007 at 02:55 AM

Amazing, I can\'t belive... really useful in every windows application.. i am going to implement same one.

so please update with all new things in .net 2.0

Thanks a lot!!!

by Upen on Monday, July 9th 2007 at 03:12 AM

oh!! i am getting this error :Exception from HRESULT: 0x8004503A

please help me how can i solve this???

by Arthur on Sunday, July 29th 2007 at 11:40 PM

Great tutorial, Ryan!
When can we expect another one?

by Dharmender on Tuesday, October 23rd 2007 at 07:34 AM

i also got this Exception : - HRESULT: 0x8004503A

by Joe on Tuesday, November 13th 2007 at 09:46 AM

I did everything exectly, but COM class not registered came up on the mobile device. Can\'t get around this error, does anyone know what to do to fix this error ?

by gamu on Friday, December 14th 2007 at 02:48 AM

Pretty good C# tutorial, i really liked it. Please continue with more interesting tutorials.

by Gargamel on Sunday, December 23rd 2007 at 11:50 PM

Very nice, added a bit of AHH to my class project

Thanks

by Dave from NY on Tuesday, January 8th 2008 at 09:02 AM

When I ran it, I got an Exception : - HRESULT: 0x8004503A

by john on Tuesday, February 19th 2008 at 09:18 AM

weee, nice, imm gooing to use this to listen to the other tuts =) dont have to read anymore, sw33t =)

by Ravi Bhavnani on Wednesday, February 20th 2008 at 09:54 AM

.NET 3.0 includes the Speech.* namespaces that make TTS a bit easier. See this article:

http://www.ravib.com/dotnetbits/article.050.TTS.htm

Cheers,

-- Ravi

by Nabil on Thursday, March 20th 2008 at 03:18 AM

hi
I am a beginner in C#
your tutorial\'s make it to easy to learn this language
Now I am gating a problem in windows programming.
when i maximize the form it get full screen but it\'s component does not follow it . they still in there previous state !
what can i do for that .
pls help me i am doing a collage project
& sorry for my bad english

by Robert KRT on Tuesday, May 13th 2008 at 04:35 AM

hello,
for Nabil: use "DOCK" for those component from the properties list of each component.:)
have a nice day

by dharn on Monday, June 30th 2008 at 06:21 AM

Really its a simple n easy code, good work ,thanks for ur code

by viki on Friday, August 8th 2008 at 08:37 AM

nice work and its really helpful for all of us which are begniers in C#..
thanks alot ..and keep it up

by ismail on Tuesday, September 2nd 2008 at 07:42 AM

it is very nice C# tutorial but i need to find out how can i can use this application in my national language?

by Ben on Monday, October 27th 2008 at 07:24 AM

I get an error at compile time saying that the name "InitializeComponent" doesn't exist in the current context.

Where is the code for that method? What am I missing?

Thanks for your help!

by Ben on Monday, October 27th 2008 at 07:28 AM

Never mind! I had the tutorial code in Program.cs, when I put it in Form1.cs, it worked fine.

Thanks for the tutorial!

by Sohail on Saturday, November 8th 2008 at 06:00 PM

Thanks for this example!! It just boosted my confidence!

by Guna on Saturday, November 15th 2008 at 12:52 AM

Exception from HRESULT: 0x80045009

i m getting this error . while using windows server 2003. i installed speech kit 5.1 in server, i m getting same error

any solution?

by chathura on Friday, September 25th 2009 at 02:23 PM

wow...dude...this is awesome

by jksdfjk on Friday, December 18th 2009 at 07:46 AM

fuck

by canh vietnam on Monday, December 28th 2009 at 05:21 AM

can you do on the Pocket PC ?
Thanh?
quangcanh.dhv@gmail.com


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Tutorials
There are no related tutorials.

Comment Related Source Code
There is no related source code.

Jobs C# Job Search
My skills include:
Enter a City:

Select a State:


Advanced Search >>
Latest Tech Bargains

Advertisement

Free Magazine Subscriptions

Today's Pictures

Today's Video

Other Resources

Latest Download

Latest Icons