how to use installed voices in c# visual studio 2010
I want to use installed voices of male, female or etc in c# program. i m using speechsynthesizer ans speakAsync functions开发者_StackOverflow. Please help me.
Here is a simple article on how to implement speech in your application:
http://www.dotnetfunda.com/articles/article828-build-your-talking-application-.aspx
As a part of the article, it shows how to list all of the installed voices and it also shows you how to then use your selected voice in your application. Here is the example code this article gives:
List lst = new List();
foreach (InstalledVoice voice in spsynthesizer.GetInstalledVoices())
{
lst.Items.Add(voice.VoiceInfo);
}
spsynthesizer.SelectVoice(lstVoice[0].Name);
This would put all of the installed voices into a List and it would use the first voice in the list as the selected voice.
If you want you'r program to speak try using this:
public void Say(string say)
{
SpeechSynthesizer talker = new SpeechSynthesizer();
talker.Speak(say);
}
And call this function like this: Say("Hello World"!);
Make sure you include: using System.Speech.Synthesis;
If you need to get a list of male or female voices you can do something like this:
private static void Main()
{
Speak(VoiceGender.Male);
Speak(VoiceGender.Female);
}
private static void Speak(VoiceGender voiceGender)
{
using (var speechSynthesizer = new SpeechSynthesizer())
{
var genderVoices = speechSynthesizer.GetInstalledVoices().Where(arg => arg.VoiceInfo.Gender == voiceGender).ToList();
var firstVoice = genderVoices.FirstOrDefault();
if (firstVoice == null)
return;
speechSynthesizer.SelectVoice(firstVoice.VoiceInfo.Name);
speechSynthesizer.Speak("How are you today?");
}
}
精彩评论