关于使用Speech SDK 5.1过程中遇到的问题
如何使用SpeechLib 来完成一个语音识别的程序,我需要的是语音识别,而不是文字到语音转换,在网上看到了很多代码是这样写的:
public class SpRecognition
{
private static SpRecognition _Instance = null;
private SpeechLib.ISpeechRecoGrammar isrg;
//private SpSharedRecoContextClass ssrContex = null;
private SpSharedRecoContextClass ssrContex;
private System.Windows.Forms.Control cDisplay; //fan:用来显示语音转化后的文本
public System.Windows.Forms.TextBox textbox; //fan:增加textbox在SpRecognition 类
public SpRecognition()
{
ssrContex = new SpSharedRecoContextClass();
isrg = ssrContex.CreateGrammar(1);
SpeechLib._ISpeechRecoContextEvents_RecognitionEventHandler recHandle =
new _ISpeechRecoContextEvents_RecognitionEventHandler(ContexRecognition);
ssrContex.Recognition += recHandle;
}
public void BeginRec(Control tbResult)
{
isrg.DictationSetState(SpeechRuleState.SGDSActive);
cDisplay = tbResult;
//cDisplay.Text = "dddddddddd"; 测试,可以成功在textbox1那里显示出来
}
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)
{
cDisplay.Text += result.PhraseInfo.GetText(0, -1, true);
}
public void MessageBegin()
{
textbox = new TextBox();
textbox.Text = "Notice :this time ,it Begin recoginse";
MessageBox.Show(textbox.Text);
}
public void MessageEnd()
{
textbox = new TextBox();
textbox.Text = "Notice :this time ,it End recoginse";
MessageBox.Show(textbox.Text);
}
}
但是程序出错:Error 1 Interop type 'SpeechLib.SpSharedRecoContextClass' cannot be embedded. Use the applicable interface instead. D:\C#学习\tryreco\tryreco\Form1.cs 48 21 tryreco
求大神解释,为什么出错了呢?怎么解决啊?
------解决方案--------------------
如果用的是visual studio 2010, 试试在solution explorer右击SpeechLib,点属性,Embed Interop Types一项选true