c# 文字語音應用-Windows內建使用
今天收到一個需求需要做一個語音系統,直覺性就聯想到google小姐,不過程式開發要怎麼做才會有google那樣的效果呢?
答案是可以微軟內部的com元件
作法:
1. 先在專案下引用元件 Microsoft Speech Object Library
2. 加入命名空間using SpeechLib;
3. 撰寫程式碼:
class SpVoiceController
{
private static Logger logger = NLog.LogManager.GetCurrentClassLogger();
public void GetVoice()
{
try
{
SpVoice voice = new SpVoice();
voice.Voice = voice.GetVoices(string.Empty, string.Empty).Item(0);//Item(0)中文女聲
voice.Speak("xxxx", SpeechVoiceSpeakFlags.SVSFDefault);
}
catch(Exception ex)
{
logger.Error(ex.ToString());
}
}
}
這裡是用中文女聲,如果有需求可以改其他聲音或是讀音:
voice.GetVoices(string.Empty, string.Empty).Item(2); //Item(2)男聲
voice.GetVoices(string.Empty, string.Empty).Item(1); //Item(1)女聲
參考文獻:
https://einboch.pixnet.net/blog/post/267347720-c%23%E7%9A%84%E6%96%87%E5%AD%97%E8%BD%89%E8%AA%9E%E9%9F%B3%E6%87%89%E7%94%A8%28text-to-speech%29
https://social.msdn.microsoft.com/Forums/en-US/c1b1188a-8c8c-408c-a0ab-fff871deed4c/microsoft-speech-object-library?forum=aspwebforms
留言
張貼留言