1. 第一步,需要三個dll
using System.Speech.Synthesis;
using System.Speech.Recognition;
using SpeechLib;
SpeechLib的導入有點不太好找,參考下圖(圖片排版不太好)
![]() |
|
2.准備個測試demo
///獲取計算機本身自帶的語言系統
public static List<string> GetInstalledVoiceIdList()
{
List<string> resultList = new List<string>();
try
{
SpVoice voice = new SpVoice();
ISpeechObjectTokens ss = voice.GetVoices(string.Empty, string.Empty);
if (ss != null && ss.Count > 0)
{
for (int i = 0; i < ss.Count; i++)
{
resultList.Add(ss.Item(i).Id);
}
}
}
catch (Exception)
{
}
return resultList;
}
//指定語言Speak方法中第二個參數,指定語言,從上段代碼中獲取到中文系統的index 是3,所以將傳遞3進行閱讀
voice.Speak(txt.Text, GetInstalledVoiceIdList()[3]);