Ziff-Davis Enterprise 
DevSource: Microsoft Developer Resource
Add OnsArchitectureLanguagesTechniquesUsing VSForums
 
Home arrow Languages arrow Text To Speech in C# (with Talking Browser!)
Text To Speech in C# (with Talking Browser!)
By Rick Leinecker

Rate This Article:
Add This Article To:
Text To Speech in C# (with Talking Browser!)
( Page 1 of 2 )

Rick Leinecker shows you how to use the Microsoft Speech API by creating a speech-enabled browser.

I was lead developer for a product named Genie Reader. It was a speech-enabled web browser, and was targeted at blind individuals who wanted to surf the web. The product presented quiet a few programming challenges, but developing it today would be much simpler. That's because the Microsoft Speech API (SAPI) has come a long way and requires far less effort to use. It's also much easier to develop C# applications than C++ applications, as the Genie Reader project was.

While Genie Reader was meant for the blind, I bet a lot of sighted folks would use it. That's because we're starting to get spoiled. Many Web sites have podcasts so that you don't have to read the page. Many times I'll choose to go to a Web site with a podcast instead of one that doesn't have a podcast. I'm like many others, I'd rather have a page read to me than have to read it myself.

ADVERTISEMENT

Even though this article talks about speech within the context of a browser, there are hundreds, if not thousands of things that can benefit from text to speech. You can provide user interface prompts as audible speech. You can issue warnings as speech. What you can do is only limited by your creativity and inventiveness.

I created a demonstration application. It's a speech-enabled browser. You can

download the project here.

Creating a Web Browser

The first thing we need to do is create a Web browser. I doubt that it could be any easier than it is. I started the TalkingBrowser demonstration application by creating a C# Windows application project. I then added five buttons across the top: Back, Forward, Home, Stop, and Go To. I also added a text box where a new URL could be entered.

I then added a Web Browser object from the Toolbox to the Form window. It takes up the remainder of the screen below the row of control buttons. I changed two properties: Anchor and Url. It's important to anchor the control properly so that when users resize, the control resizes correctly with the Form window. You'll want to make sure that you anchor the left, top, and right edges. I also set the Url property to http://www.google.com so that there's an initial Web site that displays when the application first appears.

For each button, I double-clicked and created event handlers. The first four buttons were a piece of cake. They simple called the web browsers method to perform the task. (The name of the instantiated web browser object is objWebBrowser.) For the Back button, I called the objWebBrowser.GoBack method from within the event handler; for the Forward button, I called the objWebBrowser.GoForward method from within the event handler; from the Home button, I called the objWebBrowser.GoHome method from within the event handler; and from the Stop button I called the objWebBrowser.Stop method from within the event handler.

The Go To button had a bit more code. There were three things I had to be concerned with. The first is that there was data in the text box. The code trims the URL text box and then checked for a zero length. If there is no data in the text box, the method bails out. The next thing that must be done is to insure that the Url begins with http://. If it doesn't, then the web browser object will throw an exception. The last thing in the event handler for the Go To button is to catch exceptions that may be thrown while setting the web browser's Url property.

All of the event handler code for the five buttons is shown below.

// This is fired when user
//   clicks on the back button.
//   It calls the web browser
//   object's GoBack method.
private void btnBack_Click(object sender, EventArgs e)
{
     objWebBrowser.GoBack();
}

// This is fired when user
//   clicks on the forward button.
//   It calls the web browser
//   object's GoForward method.
private void btnForward_Click(object sender, EventArgs e)
{
     objWebBrowser.GoForward();
}

// This is fired when user
//   clicks on the home button.
//   It calls the web browser
//   object's GoHOme method.
private void btnHome_Click(object sender, EventArgs e)
{
     objWebBrowser.GoHome();
}

// This is fired when user
//   clicks on the stop button.
//   It calls the web browser
//   object's Stop method.
private void btnStop_Click(object sender, EventArgs e)
{
     objWebBrowser.Stop();
}

// This is fired when user
//   clicks on the go to button.
//   It sets the web browser
//   object's Url.
private void btnGoTo_Click(object sender, EventArgs e)
{
     // Get the URL from the text box.
     string strURL = txtDestURL.Text.Trim();

     // If we have a zero length, bail
     //   out.
     if (strURL.Length == 0)
     {
          return;
     }

     // Make sure it starts with http://
     if (strURL.ToUpper().Substring(0, 7) != "HTTP://")
     {
          strURL = "http://" + strURL;
     }

     try
     {
          // Set the web browser's URL.
          objWebBrowser.Url = new Uri(strURL);
     }
     // Catch and handle the exception.
     catch (Exception ex)
     {
          MessageBox.Show(ex.Message.ToString());
     }
}

Testing the application out at this point showed me a fully functional (although simple) browser as shown in the figure below.



 
 
>>> More Languages Articles          >>> More By Rick Leinecker
 



DevSource video
Devsource Video Series
Manipulating Society through Technology
Jeremy Bailenson, Director of the Virtual Human Interaction Lab at Stanford University, talks about virtual reality, avatars, Moore's law, how real world behaviors influence online reality, and societal manipulation through technology!
>> Play video
>> Read article
>> See all videos
DevLife Blog

Julia explores the Robotics Studio! (It's for more than you think.)

MSDev Blog

Messages for Bill Gates!

Make it Work
.NET makes runtime type checking a breeze. See what Peter has to say about it in this week's tips!
News
Microsoft Counts on App Support for Vista
Microsoft has taken pains to demonstrate that Windows Vista will have ample application support.
DevSource RSS FEEDS
XML Want an easy way to keep up with breaking tech news? And the Get DevSource headlines delivered to your desktop with RSS.