在windows8 時代,應用程序使用TTS功能需要使用一個TTS web service。這樣的缺點就是必須在確保聯網的情況下才能使用TTS功能。
Windows8.1中加入了TTS(Text-To-Speech) API。使用方式如下:
先創建一個MediaElement:
<MediaElement x:Name="playTTS" />
然後代碼中調用:
using Windows.Media.SpeechSynthesis;
var speech = new SpeechSynthesizer();
var stream = await speech.SynthesizeTextToStreamAsync("Hello world.");
playTTS.SetSource(stream, "audio/wav");
playTTS.Play();
其中使用系統中自帶voice,可查看相關信息:
var name = speech.Voice.DisplayName;
var gender = speech.Voice.Gender;
var description = speech.Voice.Description;
var language = speech.Voice.Language;
var id = speech.Voice.Id;
var allVoice = SpeechSynthesizer.AllVoices;
var defaultVoice = SpeechSynthesizer.DefaultVoice;