Let's begin with a console application and add a reference to the System.Speech assembly. Once added you will also need to add the System.Speech.Synthesis namespace to your project. The primary class responsible for Text to Speech is the SpeechSynthesizer class. This class takes a "Prompt" and renders it as audio. A Prompt can either be a text or an audio file. Let's take a look at a quick example using the famous "Hello world" as an example text.
- Prompt textPrompt = new Prompt("Hello World");
- SpeechSynthesizer speechSynthesizer = new SpeechSynthesizer();
- speechSynthesizer.Speak(textPrompt);
- ReadOnlyCollection<InstalledVoice> installedVoice = speechSynthesizer.GetInstalledVoices();
- foreach (InstalledVoice voice in installedVoice)
- {
- Console.WriteLine(voice.VoiceInfo.Name);
- }
- speechSynthesizer.SetOutputToWaveFile("file name.wav");
- class Program
- {
- static void Main(string[] args)
- {
- SpeechSynthesizer speechSynthesizer = new SpeechSynthesizer();
- speechSynthesizer.SpeakProgress += new EventHandler<SpeakProgressEventArgs>(speechSynthesizer_SpeakProgress);
- speechSynthesizer.Speak("The quick brown fox jumps over the lazy dog");
- Console.ReadKey();
- }
- static void speechSynthesizer_SpeakProgress(object sender, SpeakProgressEventArgs e)
- {
- Console.WriteLine(e.Text );
- }
- }
No comments:
Post a Comment