Tuesday, 3 July 2012

Speech in you Windows XNA Games

I found this while messing about with the media player in XNA, just noticed there is some legacy code in the download from it :( Anyway, how to get speech into your games, it's dead easy really, you just need to have .NET Framework 3.0 and you have all you need.

NOTE: You don't need XNA 3.0 for this just the .NET 3.0 Framework on your system.

First off you need to add the System.Speech assembly to your project references, then in your Game1.cs add the following:

using System.Speech.Synthesis;

and in your Game1 class add:

SpeechSynthesizer synth = new SpeechSynthesizer();

Now, all you have to do is call the synth.SpeackAsync() method passing the text you want to have spoken. I used this:

synth.SpeakAsync("Hello, Welcome to the Random Chaos Generic X N A Speech Sample." +
" This sample is hosted by; the X N A U K User Group. Thanks " +
"to Leaf and Robin for letting me host my blog here and I hope " +
"you find my samples useful.");

This will use the default voice that is set up on your PC (see Control Panel->Speech). But you can also access the other two voice types that ship with the OS (XP, not sure about Vista). To do this try any one of these:

synth.SelectVoice("LH Michael");
synth.SelectVoice("LH Michelle");
synth.SelectVoice("Microsoft Sam");

I prefer "LH Michael", reminds me of Stephen Hawking and I think it's a little clearer than the other two.

I know, not the best chat in the world, but it's free 

No comments:

Post a Comment