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):
- Create a new Windows Application project.
- Under the project, add a new reference. Select System.Speech from the .NET tab.
- Create a Windows Form, and add a button to it. Name the button btnSpeak.
- [Code Line 1] Add a reference to the System.Speech.Systhesis namespace using the using keyword.
using System.Speech.Synthesis;
- [Code Line 2] Create an instance variable for a SpeechSynthesizer object.
private SpeechSynthesizer mySpokenWords = new SpeechSynthesizer();
- Create an event handler for the btnSpeak's Click event.
- [Code Line 3] Add the following code to the Click event handler:
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!");
}
}
}