Hello world to TTS (2)
In this post, I will show you how to use TTS with the managed Speech API (System.Speech.dll) in C#. This API is introduced since Windows Vista.
It supports the tranditional SAPI engines and SSML
The steps are as below.
1. create a console C# application.
2. add referrence to System.Speech.dll.
3. input the sample code below:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Speech.Synthesis;namespace SystemSpeech
{
class Program
{
static void Speak(int voiceIdx)
{
SpeechSynthesizer syn = new SpeechSynthesizer();
ReadOnlyCollection<InstalledVoice> voices = syn.GetInstalledVoices();
if (voiceIdx < 0 || voiceIdx >= voices.Count)
{
Console.WriteLine("voice index out of range from [0-{0}]", voices.Count);
return;
}syn.SelectVoice(voices[voiceIdx].VoiceInfo.Name);
syn.Speak("Hello world");
}static void ListVoice()
{
SpeechSynthesizer syn = new SpeechSynthesizer();
ReadOnlyCollection<InstalledVoice> voices = syn.GetInstalledVoices();
foreach (InstalledVoice voice in voices)
{
Console.WriteLine(voice.VoiceInfo.Description);
}
}static void Main(string[] args)
{
// list voice installed on the system
ListVoice();// speak with the second voices. If you have only one voice, change it to Speak(0)
Speak(1);
}
}
}