Silverlight 4: Convert Text to Speech

In this post, we will see how to use Silverlight and COM to convert text into speech. We will be using the  the ‘SAPI.SpVoice’ API. To start with, let’s create a Silverlight Project using Microsoft Expression Blend as shown below –

Silverlight app

Let’s name the project as ‘TextToSpeechExample’ and click on ‘OK’ button. Now let’s design a screen as shown below –

Text to Speech Example

In the above screen, I have taken four buttons and one Textblock. Replace the <Grid></Grid> with the following code –


Now import a namespace ‘using System.Runtime.InteropServices.Automation;’ and add a reference of ‘Microsoft.CSharp.dll’ file from the path – ‘C:\Program Files (x86)\Microsoft SDKs\Silverlight\v4.0\Libraries\Client’.

Now let’s write a code on each button click as shown below:


In the code shown above, we are using ‘SAPI.SpVoice’ API which brings text-to-speech (TTS) engine capabilities to applications using SAPI automation. In our demo, we are using a ‘Volume’ property and ‘Speak’ method which accepts text and converts it to Speech.

Now right click the Silverlight project and click on ‘Properties’. From the properties window check the checkbox – ‘Enable running application out of browser’ and click on the button below called ‘Out-Of-Browser settings’. This will show you a dialog box. Check the check box ‘Require evaluated trust when running outside the browser’ as shown below –

Require Elevated Trust

Now hit ‘F5’, right click the Silverlight application on the browser and install the application as shown below –


This brings up a security warning dialog box. Install the application and click the buttons. You will see that the text stories get converted to speech. Nice!

Download the source code

No comments: