We will use Microsoft Visual Studio 2010,
Silverlight 4, and the Windows 7 SDK to develop this sample.
First, let's navigate to Visual Studio.
- Begin by creating a new Silverlight
application by navigating to File->New Project->Silverlight
Application template.
- Next, add reference to the
Microsoft.CSharp namespace. To do this, right-click the application
in Solution Explorer and click Add Reference, and then
select Microsoft.CSharp in the .Net tab.
The Microsoft.CSharp namespace contains classes that
support compilation and code generation using the C# language.
- Now, add a Button control to the
MainPage.xaml file. Add some sample text to its content.
Also, register a click event handler for this button.
- Then, navigate to the event handler of
this button.
- In this file, add a reference to the
System.Runtime.InteropServices.Automation namespace. It
provides classes that enable Silverlight-based applications to communicate with Automation APIs.
- Let's handle the click event of the
button. In this, start a try-catch block. Now, in the try block, create a
dynamic object named ISpeechVoice by calling the
CreateObject() method of the AutomationFactory
class. Also, pass the SpVoice object of the speech API. The
SpVoice object brings the text-to-speech engine capabilities to the
applications using SAPI automation. Here, you can set various fundamental
characteristics of the voice. Let's set its volume to 100. Then, call the
Speak() method of this class. The
Speak
method initiates the speaking of a text string, text file, or a wave file.
Also, pass some sample text to be converted into speech in this method.
Next, in the catch block, add a message box that displays the error message.
- Since we are developing an Out-of-browser
application, let's make this application run out of the browser. To do
this, right-click the application in the solution explorer and click
Properties. On this page, click the Silverlight
tab in the left pane. Check the Enable running out of the browser
check box. Click the Out-of-browser settings
button. In this, click the Required Elevated trust when running out
of the browser check box. Then, click OK.
- Now, run the application. The application
runs in a separate window, out of the browser and a button is displayed in
the window. Click the button. You can hear the text that was entered in the
application.
This sample demonstrates how you can create
a Silverlight Out-of-browser application and use the Microsoft Speech API to
convert text to speech.