C#实现文本语音互转
下载SAPI:
1)SpeechSDK51.exe
2)SpeechSDK51LangPack.exe
下载地址
文字转语音:
1)在COM选项卡里面的Microsoft Speech object library引用
2)using SpeechLib;
3)SpVoiceClass voice = new SpVoiceClass();//SAPI 5.1
SpVoice voice = new SpVoice();//SAPI 5.4
4)voice.Voice = voice.GetVoices(string.Empty, string.Empty).Item(3);
5)voice.Speak("你好世界");
语音转文字:
public class SpRecognition
{
private static SpRecognition _Instance = null;
private SpeechLib.ISpeechRecoGrammar isrg;
private SpeechLib.SpSharedRecoContextClass ssrContex = null;
public delegate void StringEvent(string str);
public StringEvent SetMessage;
private SpRecognition()
{
ssrContex = new SpSharedRecoContextClass();
isrg = ssrContex.CreateGrammar(1);
SpeechLib._ISpeechRecoContextEvents_RecognitionEventHandler recHandle =
new _ISpeechRecoContextEvents_RecognitionEventHandler(ContexRecognition);
ssrContex.Recognition += recHandle;
}
public void BeginRec()
{
isrg.DictationSetState(SpeechRuleState.SGDSActive);
}
public static SpRecognition instance()
{
if (_Instance == null)
_Instance = new SpRecognition();
return _Instance;
}
public void CloseRec()
{
isrg.DictationSetState(SpeechRuleState.SGDSInactive);
}
private void ContexRecognition(int iIndex, object obj, SpeechLib.SpeechRecognitionType type, SpeechLib.ISpeechRecoResult result)
{
if (SetMessage != null)
{
SetMessage(result.PhraseInfo.GetText(0, -1, true));
}
}
}