syrian developers community
Speaking Computer in C#
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.

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.

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!
| Print article | This entry was posted by moustafa farhat on 26/11/2009 at 9:19 pm, and is filed under .net. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
