Speech in 3 Lines of Code with the .NET Framework 3.0 and Windows Vista

To do this, you will need:

1) Windows Vista (prefereably, RC2 or higher). This ships with the .NET Framework 3.0 included.

Perform the following steps in Visual Studio 2005 (C# shown):

  1. Create a new Windows Application project.
  2. Under the project, add a new reference. Select System.Speech from the .NET tab.
  3. Create a Windows Form, and add a button to it. Name the button btnSpeak.
  4. [Code Line 1] Add a reference to the System.Speech.Systhesis namespace using the using keyword.
  5. using System.Speech.Synthesis;
  6. [Code Line 2] Create an instance variable for a SpeechSynthesizer object.
  7. private SpeechSynthesizer mySpokenWords = new SpeechSynthesizer();
  8. Create an event handler for the btnSpeak's Click event.
  9. [Code Line 3] Add the following code to the Click event handler:
  10. mySpokenWords.Speak("Hello World!");

 

Compile and run the code. Click on the button and listen to the result.

That's all there really is to it!

Total Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Speech.Synthesis;

namespace SpeakHelloWorld
{
    public partial class Form1 : Form
    {
        private SpeechSynthesizer mySpokenWords = new SpeechSynthesizer();

        public Form1()
        {
            InitializeComponent();
        }

        private void btnSpeak_Click(object sender, EventArgs e)
        {
            mySpokenWords.Speak("Hello World!");
        }
    }
}
Published Tue, Oct 17 2006 14:25 by Dons
Filed under: ,

Leave a Comment

(required) 
(required) 
(optional)
(required)